Given a Unicode codepoint in integer form, I want to convert it to the standard U+ hexadecimal notation. The simple code snippet below achieves this, but perhaps there’s a better/simpler way to do it in OCaml ?
The closest I could find was Printf.prinf “%x”, but this only prints to stdout, and does not return a string.
let decompose_in_hex_base n=
let rec tail_rec_decomposer=(
fun (results,walker)->
if walker=0
then String.concat "" results
else
let remainder=(walker mod 16)
and new_walker=(walker / 16) in
let char_index=(
if remainder<=9
then 48+remainder
else 55+remainder
) in
let result=String.make 1 (char_of_int char_index) in
tail_rec_decomposer(result::results,new_walker)
) in
tail_rec_decomposer ([],n);;
let unicode_point i=
let temp=decompose_in_hex_base i in
let offset=4-String.length(temp) in
if offset>0
then "U+"^(String.make offset '0')^temp
else "U+"^temp;;