NYCPHP Meetup

NYPHP.org

[nycphp-talk] UCS2/HexEncoded chracters

Michael B Allen ioplex at gmail.com
Wed Dec 9 14:13:57 EST 2009


Hi Mohammad,

Use the iconv function like:

  $outstr = iconv('UTF-8', 'UCS-2BE', $instr);

The iconv function might be an extension but it's been available on
every PHP installation I have ever used.

You can also test the conversion using the 'iconv' commandline program like:

  linux$ iconv --from-code=UTF-8 --to-code=UCS-2BE /tmp/infile > /tmp/outfile

Of course you'll need to take the necessary precautions to ensure that
the input is UTF-8. So if for example the input is coming from an HTML
form, you will need to ensure that the content-encoding of the page is
UTF-8. Otherwise, if it is something like ISO-8859-1 you will get the
wrong result with non-Latin characters such as Arabic.

Also if you run the above commandline example in a terminal, it may
not have the required font to render the characters. But you can use
the 'hexdump' program to dump the outfile to hexadecimal and verify
that the conversion worked.

Finally, note that Windows uses UTF-16LE and Java uses UCS-2BE for
native encodings. Some of these encodings may look similar in fact
they are not the same. You may want to double check what encoding
you're really targeting.

Mike

2009/12/9 Mohammad Abu Karim <mabukarim at gmail.com>:
> Hi nyphp members,
>
> I am new member in this mailing list, and I am happy to join it.
>
> Any one can help me ? how could I get UCS2/HexEncoded chracters
>
> like 'Hello' will return "00480065006C006C006F"
>
> This are the HexEncoded values:
>
> 0048 = H
> 0065 = e
> 006C = l
> 006C = l
> 006F = o
>
> Also in arabic (!مرحبا عالم) will return
> 06450631062d0628064b06270020063906270644064500200021
>
> How I can get the encoded UCS2 in php?
>
> Regards
>
> --
> ---
> Mohammad Abdallah
>
> _______________________________________________
> New York PHP Users Group Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> http://www.nyphp.org/Show-Participation
>



-- 
Michael B Allen
Java Active Directory Integration
http://www.ioplex.com/



More information about the talk mailing list