Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

View Unicode in hex

by graff (Chancellor)
on Sep 10, 2004 at 06:00 UTC ( #389970=snippet: print w/ replies, xml ) Need Help??

Description: Maybe you don't have the right display tool to show Unicode text, or maybe you need to see details that a standard character display can't show you, or maybe you just want to see the 4-hex-digit code point value for some wide characters. This will show you the 4-hex-digit details.
# assuming 5.8.1 or later:

perl -C9 -pe 's/([^[:ascii:]])/sprintf("\\x{%.4x}",ord $1)/eg' sometex
+t.utf8

# that is, leave ascii data as-is, convert wide characters to "\x{HHHH
+}"
If you leave off "-C9", you get to see the "underlying" utf8 byte sequences (in which case, a format of "\\x{%x}" would suffice), instead of the "official unicode reference" 4-hex-digit utf16 values. The "-C9" (same as "-CIi") causes STDIN and any input file handle to be read/interpreted as utf8 characters (i.e., works when reading from a pipe as well as from a file).

In fact, leaving off "-C9" makes it behave sort of like a typical hex-dump utility, except that only the bytes whose 8th bit is set will be converted to hex.

If your input is in some encoding other than utf8 and you want to see which unicode characters it will be turned into, do it like this (using Cyrillic as an example):

perl -pe 'BEGIN{binmode STDIN,":encoding(iso-8859-5)"} s/([^[:ascii:]]
+)/sprintf("\\x{%.4x}",ord $1)/eg' < iso-cyrillic.txt
Comment on View Unicode in hex
Select or Download Code
Re: View Unicode in hex
by gaal (Parson) on Sep 10, 2004 at 06:56 UTC
    Hey, funny, just the other day I whipped this up:

    perl -wpC -e 's/(.)/sprintf "U+%04X ", ord $1/eg'

    Which is essentially the same, but your version is better because it leaves the ASCII data alone.

      4343

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://389970]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-08-31 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls