in reply to UCS2/HexEncoded

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

Replies are listed 'Best First'.
Re^2: UCS2/HexEncoded
by bart (Canon) on Feb 17, 2005 at 23:16 UTC
    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.
Re^2: UCS2/HexEncoded
by TedYoung (Deacon) on Feb 17, 2005 at 20:39 UTC

    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

Re^2: UCS2/HexEncoded
by Anonymous Monk on Feb 18, 2005 at 13:47 UTC
    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