The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
why convert 8 space to 1 tab character on unix? Tlg13team Shell Programming and Scripting 6 08-20-2008 12:06 AM
Convert character in word to CAPS?? vadharah Shell Programming and Scripting 3 04-01-2008 08:44 AM
sed replace encoded string mg1 UNIX for Dummies Questions & Answers 5 01-10-2008 12:31 PM
convert special character like £ cynnie Shell Programming and Scripting 1 08-08-2007 07:37 AM
utf-8 encoded string and awk alina Shell Programming and Scripting 2 07-16-2007 10:22 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-28-2008
sumirmehta sumirmehta is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 16
Convert UTF-8 encoded hex value to a character

Hi,
I have a non-ascii character (Ŵ), which can be represented in UTF-8 encoding as equivalent hex value (\xC5B4). Is there a function in unix to convert this hex value back to display the charcter ?
  #2 (permalink)  
Old 10-28-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,934
If your locale is set up correctly any number of utilities can display this character correctly. For example if your shell is ksh93 version s or better, printf "\xC5B4" will output the expected character.
  #3 (permalink)  
Old 10-28-2008
sumirmehta sumirmehta is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 16
Hi fpmurphy, thanks for the response.
can u pls elaborate on how and what locale do is set ? i am operating on solaris 5.8 OS.

also this is a part of the bigger problem. Actually i am transmitting the above character through a email message. so i extract this message from the mail server in unix, and decode it. What happens here is that the character is decoded as (Å´), which is actually 0xC5B4 annoted in ascii ( 0xC5 = Å, 0xB4 = ´).

So i want to take these (Å´) and convert to (Ŵ) directly or via their hex value (\xC5B4).
  #4 (permalink)  
Old 10-28-2008
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
That means the decoding process treats the message as ISO-8859-1 (or ASCII) rather than UTF-8. There is no "conversion" going on here. It's simply the decoding process fails based on a wrong assumption of encoding.

Have you tried to investigate if anything is wrong that causes the message not to be interpreted as UTF-8? For instance, did you check the encoding in the mail header, was it erroneously specified as anything other than UTF-8? And you may try with other mail with UTF-8 and see if that is an issue with a particular mail (sometimes a misconfigured mail user agent is culprit) or a bigger issue. Try switching mail clients and see if you can always reproduce that.
  #5 (permalink)  
Old 10-28-2008
sumirmehta sumirmehta is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 16
Hi there,
I am actually using perl to retrieve message from the mailbox. the perl module for encoding/decoding (MIME-Base64-3.07 > MIME::Base64) is the one to be used, but while decoding it does decode to ascii/iso-8859-1 (while the mail header correctly shows the encoding as UTF-8).

In this case, if i want to convert this data to utf-8 back (as detailed above) , is there a command/way to do it in unix ?
  #6 (permalink)  
Old 10-28-2008
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
I am not too sure about MIME::Base64 as I have not used it before. However, base64 itself is encoding-agnostic, that is, it encodes/decodes without regard to whatever encoding the original message is, because it is not only used to encode textual data, but also images, zip files or just about any binary data you can imagine, that do not have the notion of an "encoding" at all. So what Base64 sees and acts on, is just a bytestream. it doesn't really care what is inside.

So, for a text message:


Code:
            encoding      Base64 Encoding
Text content ---> bytestream ---> Base64-encoded message

               Base64 decoding      Decoding
Base64-encoded message ---> bytestream ---> Text content

In other words, you still need to manually handle the decoding to have Perl decode it as UTF-8 properly. By default, Perl treats everything as ASCII, so that may explain why you get the output wrong.

Perl has specific quirks with respect to Unicode. That really much depends on the version of Perl you are using. I have had a rather thorough investigation of Perl Unicode support in 5.8 branch, but not sure if any changes have been implemented in 5.10. If you have Perl 5.6 or earlier, chances are the Perl Unicode support is not adequate to ensure Unicode-safety.

I am unable to explain so much with so little space here. I recommend you start with the perluniintro manpage for further information:

perluniintro - perldoc.perl.org

You will need to provide more information in what is going on in the Perl side if you would like to pursue this in a more constructive manner.

Last edited by cbkihong; 10-28-2008 at 10:48 PM.. Reason: typo
  #7 (permalink)  
Old 10-28-2008
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
Ok, in case you feel bewildered by that manpage (you probably will!), let me give you a series of examples to give you a general idea of some of the most important things you need to know.

As I'm Chinese, I'll use Chinese in the examples. All the code are in UTF-8.

Expected environment: a UTF-8 terminal with proper fonts to render Unicode text.

Test 1 - Let's start with this

Code:
my $str = "你好吗?";

print("$str\n");
printf("Length: %d\n", length($str));

你好吗?
Length: 12

This is made up of 4 Chinese characters, 3 bytes each in UTF-8. So, because Perl does not treat it as UTF-8 but rather ASCII, the length returned is 12. The terminal still renders the string properly because the bytes are returned verbatim to the terminal and the terminal tries to decode the bytestream as UTF-8, remember I assumed the terminal is properly configured to UTF-8 (but not Perl in this case)?

Test 2 - Recognize UTF-8 characters embedded in source code

Code:
use utf8;

my $str = "你好吗?";

print("$str\n");
printf("Length: %d\n", length($str));

Wide character in print at test.pl line 6.
你好吗?
Length: 4

Perl now recognizes the string as a 4-character UTF-8 string, but a warning is issued by Perl, because the output stream (stdout) is not configured to accept UTF-8 decoded strings.

Test 3 - Turn on UTF-8 mode on standard streams

Code:
use utf8;
binmode(*STDOUT, ":utf8");

my $str = "你好吗?";

print("$str\n");
printf("Length: %d\n", length($str));

Now the warning disappears. From the perspective of Perl, UTF-8 is now correctly handled.

But what about strings originated elsewhere (as in your case), rather than embedded in source code? We will need another way.

Test 4 - Use manual decoding

Code:
use Encode;
binmode(*STDOUT, ":utf8");

my $str = "你好吗?";
$str = decode('utf8', $str);

print("$str\n");
printf("Length: %d\n", length($str));

Same result as Test 3, but the decoding is manual. The source code is considered ASCII-encoded, and hence the string literal embedded, but the manual decode allows the decoding of the string literal back to a Perl UTF-8 string, so length() correctly reports the length afterwards.

These examples cover may be 80% of what you will need to know to have Perl process Unicode properly in the majority of cases. For the rest, you will need to consult the manpage.
Closed Thread

Bookmarks

Tags
perl, unicode

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0