in reply to How to support Unicode for Embeded Perl

What character encoding are you using when you write your "samp.pl" file? I don't know Japanese, but the only way I could get a seemingly correct display of your 5-character Japanese string was by setting my browser to use Shift-JIS, which is quite different from utf8.

I'm puzzled about the output result that you are reporting; it may be that some of the output bytes were not rendered as visible characters, and when you posted that string, some bytes might have been left out. In any case, for a 10-byte (5 shiftjis character) string to become a 28-byte (or longer?) string would probably require more than just the one call to bytes_to_utf8(). There may be more problems elsewhere in your code, involving more misunderstandings about character encodings.

I also don't know anything about Embedded Perl, so I wouldn't know whether you can  use Encode; in that environment. If you can, then probably what you need to do is something like:

use Encode; binmode STDOUT, ":utf8"; $_ = decode( "shiftjis", "こんにちは " ); print;
Note that Encode::decode( "shiftjis", "..." ) does something very different from what bytes_to_utf8() does. The latter (I expect) assumes that the string being passed as input is actually iso-8859-1, and converts it to utf8 accordingly. If the string is actually shiftjis, then the bytes are all being misinterpreted and the result will not be Japanese.

You should also make sure that the device you are printing to supports the display of utf8 characters. If it handles shiftjis, maybe you just want to skip the "bytes_to_utf8()" thing. There are very good reasons for converting to utf8, especially when dealing with Asian text data (e.g. it's much better to do regex matches, substitutions, index(), substr(), lenth() etc. with character semantics rather than byte semantics), and in general, switching to unicode is just a good idea anyway, but if your display device gives you a choice, and you are just pushing strings to a display, maybe you don't need utf8.

It's good to have some diagnostic tools when working with unicode data, to make sure the data really is unicode, and to know what's in it. I've posted a couple of tools here at the Monastery that might be helpful for you: tlu -- TransLiterate Unicode and unichist -- count/summarize characters in data.

Replies are listed 'Best First'.
Re^2: How to support Unicode for Embeded Perl
by Anonymous Monk on Oct 10, 2007 at 12:28 UTC
    Thanq for ur prompt response...What u have seen the characters are not exact(those are Japanese characters of Hello)...this browser doesn't show exactly what i have typed/copied...For getting the right characters please use online translators(http://babelfish.altavista.com/) Please refer the following for "Embeding Perl interpreter in C program" http://72.14.235.104/search?q=cache:ycFwwiAguTgJ:search.cpan.org/perldoc%3Fperlembed+embeded+Perl&hl=en&ct=clnk&cd=3&gl=in we define some API for our application. For print in perl, we have "DispText". In sample.pl contains the following ShowText("こんにちは"); # print "Hello"; After parsing the pl file, that string should be stored in wchar* variable which will be displayed by my vc++ application.When it is parsed I'm not getting the exact unicode characters through the following functions. bytes_to_utf8((U8*)SvPV(ST(0), Len), &Len); or sv_utf8_upgrade(ST(0)); Regards, nag