How can I encode strings on UTF-16BE format in PHP? For “Demo Message!!!” the encoded string should be ‘00440065006D006F0020004D00650073007300610067006’. Also, I need to encode Arabic characters to this format.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First of all, this is absolutly not UTF-8, which is just a charset (i.e. a way to store strings in memory / display them).
WHat you have here looks like a dump of the bytes that are used to build each characters.
If so, you could get those bytes this way :
And you’d get the following output :
But, once again, this is not UTF-8 : in UTF-8, like you can see in the example I’ve give, `D` is stored on only one byte : `0x44`
In what you posted, it’s stored using two Bytes :
0x00 0x44.Maybe you’re using some kind of UTF-16 ?
EDIT after a bit more testing and @aSeptik’s comment : this is indeed UTF-16.
To get the kind of dump you’re getting, you’ll have to make sure your string is encoded in UTF-16, which could be done this way, using, for example, the
mb_convert_encodingfunction :Then, it’s just a matter of iterating over the bytes that make this string, and dumping their values, like I did before :
And you’ll get the following output :
Which kind of looks like what youy posted 🙂
(you just have to remove the space in the call to
printf— I let it there to get an easier to read output=)