Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I often need to print out a UTF-8 string in a readable form. I use the following command line to do it. Any suggestions on how to make the line shorter?
perl -ne "print join(' ',map{sprintf(qq/U+%04X/,$_)}unpack('U*',$_));" + infile.txt
In addition, I often need to do this sort of thing in the debugger. Any suggestions on saving my having to retype it all the time in the debugger would be gratefully received.

Edit Masem 2001-08-03 - CODE tags

Replies are listed 'Best First'.
Re: Unicode Golf
by mirod (Canon) on Aug 03, 2001 at 15:42 UTC

    Sorry, I don;t do golf ;--(

    OK, so it's not Perl, but it is fast and compact:

    iconv -f utf8 -t latin1 infile.txt

    icon is available for most systems, including Linux, Solaris and Windows.

    If you want to do it in the debugger you can use Text::Iconv:

    # you only have to type these 2 lines once (or put them in the code) use Text::Iconv; $conv = Text::Iconv->new("utf8", "latin1"); # there you go! print $conv->convert("Text to convert");

    Note that iconv allows you to convert to and from way more than just latin1, just type iconv --list for a (long) (VERY long) list of supported character sets.

Re: Unicode Golf
by chipmunk (Parson) on Aug 03, 2001 at 17:52 UTC
    Here's a somewhat shorter way of doing it in Perl: perl -ne'printf"U+%04X ",$_ for unpack"U*",$_' infile.txt (For Windows, change the single quotes to double quotes, and vice versa.)
Re: Unicode Golf
by John M. Dlugosz (Monsignor) on Aug 04, 2001 at 01:14 UTC
    I reciently discovered:
    sprint "%vx", $_;