Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^4: How do I convert a sequence of hexes (D0 D6) to Chinese characters (中)?

by dstrom (Initiate)
on Jun 07, 2011 at 09:05 UTC ( #908434=note: print w/ replies, xml ) Need Help??


in reply to Re^3: How do I convert a sequence of hexes (D0 D6) to Chinese characters (中)?
in thread How do I convert a sequence of hexes (D0 D6) to Chinese characters (中)?

Sorry, my last post was unclear. I meant the program by grantm. Here is a clarified version:

I am very grateful for your help. However, when I run the program below by grantm, I do not get the character '中', but rather the non-sensical "Σ╕". Any idea of what is going wrong? Thanks. (I have the East Asian language pack installed, so it is not simply that.)

#!/usr/bin/perl use strict; use warnings; use Encode qw(decode); my $ascii_hex = 'D6D0'; # continue for as many bytes as required my $bytes = pack('H*', $ascii_hex); my $character_string = decode('gb2312', $bytes); binmode(STDOUT, ':utf8'); print $character_string, "\n";


Comment on Re^4: How do I convert a sequence of hexes (D0 D6) to Chinese characters (中)?
Download Code
Replies are listed 'Best First'.
Re^5: How do I convert a sequence of hexes (D0 D6) to Chinese characters (中)?
by Anonymous Monk on Jun 07, 2011 at 10:18 UTC
    I do not get the character .... Any idea of what is going wrong?

    Your thing responsible for drawing characters doesn't understand utf8.

    Neither does mine :) but od -tacx1 shows the correct bytes

    0000000   d   8   -  cr  nl
              Σ   ╕     \r  \n
            e4 b8 ad 0d 0a
    0000005
    
Re^5: How do I convert a sequence of hexes (D0 D6) to Chinese characters (中)?
by grantm (Parson) on Jun 13, 2011 at 11:36 UTC

    My example script simply printed out UTF-8 characters on STDOUT. In my case I ran the script in GNOME terminal under Linux and the correct character was displayed. Under Linux, your $LANG environment variable would typically need to be set to a UTF-8 locale for this to work (mine is set to "en_NZ.UTF-8"). If you were trying to run the script on Windows, it is very unlikely the command window will correctly display UTF-8.

    But printing to STDOUT is only one option for output. You might chose to write to a file or a database or even a network socket. If you write to a file, you should be able to open and view the file in a web browser - all web browsers understand UTF-8.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2015-08-01 02:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (285 votes), past polls