2
$\begingroup$

I'm using Mathematica10.0.2 on my Win7. Recently I crawled some websites by mathematica and within the information returned by URLFetch there are plenty of UTF-16M codes. These UTF-16M codes are actually the names of web user and I intended to translate them into normal words or characters.

a sample of UTF-16M codes looks like

"\u5929\u4e0a\u4eba\u95f4"

(If you manipulate the string above the Mathematica might warning with a heap of "Unknown string escape \u." But this is not the key point)

There are online tools to convert UTF-16 codes to the TEXT, here is a example: http://macchiato.com/unicode/convert.html

If you type the codes such as 5929 or 4e0a or 4eba or 95f4 into the website above, the Output would give you the corresponding word/characters.

My problem is how could I do this by Mathematica automatically?

I tried

 ImportString["5929", "Character16"] 

It gave wrong result.

tried

 FromCharacterCode[
 ToCharacterCode[
 ImportString[
 "\\u5927", {"Character16"}]], #] & /@
 $CharacterEncodings

still all same wrong results

 FromCharacterCode[
 ToCharacterCode[
 "\\u5927"], #] & /@ $CharacterEncodings

didn't work...

What should I do?

(*Err something like URLFetch["http://macchiato.com/unicode/convert.html","Method"->"POST",...] might be too awkward *)

$\endgroup$
5
$\begingroup$

I'm going to assume you want this specific char:

Unicode Han Character

The issue is not with Mathematica's character code conversion, but instead the encoding source.

The value you have obtained is in Hex, where you need the decimal to use FromCharacterCode.

Hex-> Dec-> Front end

FromCharacterCode[Interpreter["HexInteger"]["5929"]]

Interp

References

78666

$\endgroup$
  • $\begingroup$ Great find, I tried, but the only relevant search result I found from "UTF-16M" was the converter linked to in the OP $\endgroup$ – Jason B. Apr 26 '16 at 18:36
  • $\begingroup$ you don't need to search "UTF-16M", just search UTF-16. @JasonB $\endgroup$ – Harry Apr 27 '16 at 5:10
2
$\begingroup$

You can also use FromDigits:

FromCharacterCode[FromDigits[#, 16] &
  /@ StringSplit["\u5929\u4e0a\u4eba\u95f4", "\u"]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.