Home > other >  Create raw unicode character from hex string representation
Create raw unicode character from hex string representation

Time:04-10

Given the string hex representation of a Unicode character, I would like to print the Unicode character it represents

julia> s1, s2 = raw"\u0041", raw"\x41"
julia> println(s1,' ', s2)
\u0041 \x41

Instead, I want it to print "A A"

Use Case: For a demo, I would like to loop over a range of values and print a map from the hex representation to the Unicode:

for ii = 0x0021 : 0x007f
    hex_rep = string(ii, base=16)
    unicode = raw"\u" * lpad(hex_rep, 4, '0')
    println(hex_rep, " -> ", unicode)
end

CodePudding user response:

You can just do:

julia> Char.(0x0021 : 0x007f)
95-element Vector{Char}:
 '!': ASCII/Unicode U 0021 (category Po: Punctuation, other)
 '"': ASCII/Unicode U 0022 (category Po: Punctuation, other)
 '#': ASCII/Unicode U 0023 (category Po: Punctuation, other)
 ⋮
 '}': ASCII/Unicode U 007D (category Pe: Punctuation, close)
 '~': ASCII/Unicode U 007E (category Sm: Symbol, math)
 '\x7f': ASCII/Unicode U 007F (category Cc: Other, control)

For other cases consider using the forementioned unescape_string

  • Related