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

Hi, i want to know how to make UCS2/HexEncoded characters like Hello will be : 00480065006C006C006F 0048 = H 0065 = e 006C = l 006C = l 006F = o thank you

Replies are listed 'Best First'.
Re: UCS2/HexEncoded
by Limbic~Region (Chancellor) on Feb 17, 2005 at 20:29 UTC
    Anonymous Monk,
    The following should do what you are want:
    print UCS2( 'Hello' ); sub UCS2 { return join '', map { sprintf("00%X",ord) } split //, $_[0] + }

    Cheers - L~R

    Thanks to bart for pointing out that the U in UCS2 is for Unicode. Requires modifying the sprintf format accordingly
      Better, with regards to Unicode (and after all, this is what UCS2 is about):
      sub UCS2 { return join '', map { sprintf "%04X", ord } split //, $_[0] + } print UCS2 ("Hello\x{1234}");
      Result:
      00480065006C006C006F1234
      As you can see, it works flawlessly with characters with a character code above 255.

      Hi, what if a char is greater than FF (i.e. unicode), than shouldn't it be 0100, 0101, etc? In that case you would need to make the following change to L~R's code:

      print UCS2( 'Hello' ); sub UCS2 { return join '', map { sprintf("%02X",ord) } split //, $_[0] + }

      The only change is in the sprintf format.

      Ted Young

      ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
        TedYoung,
        I thought about that but wasn't sure what UCS2 was or what ranges were valid. Too bad the question wasn't asked better. Incidently, the process can be reversed:
        sub decode { return join '', map { chr( hex ) } unpack( 'A4' x (length +($_[0]) / 4), $_[0] ) }

        Cheers - L~R

      Hello ,

      thanks for replaying .. codes are working fine with English

      but what i want from it to support all languages

      "UCS2 is 16 bit coded characters" It is in use in countries that needs more then the standard 7 / 8 bit
      to code their character set

      bye