如何将字符串转换为 unicode?

Pro*_*fer 1 javascript unicode unicode-string node.js

我有我的一篇文字

\n\n
Japanese:  \xe3\x81\x93\xe3\x81\xaeOTP\xe3\x82\x92\xe4\xbd\xbf\xe7\x94\xa8\xe3\x81\x97\xe3\x81\xa6Quik\xe3\x83\x89\xe3\x83\xa9\xe3\x82\xa4\xe3\x83\x96\xe3\x81\xab\xe3\x83\xad\xe3\x82\xb0\xe3\x82\xa4\xe3\x83\xb3\xe3\x81\x97\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82 \xe3\x81\x93\xe3\x81\xaeOTP\xe3\x82\x92\xe8\xaa\xb0\xe3\x81\xa8\xe3\x82\x82\xe5\x85\xb1\xe6\x9c\x89\xe3\x81\x97\xe3\x81\xaa\xe3\x81\x84\xe3\x81\xa7\xe3\x81\x8f\xe3\x81\xa0\xe3\x81\x95\xe3\x81\x84\n
Run Code Online (Sandbox Code Playgroud)\n\n

我有它的unicode转换

\n\n
Unicode:  3053306e004f0054005030924f7f752830573066005100750069006b30c930e930a430d6306b30ed30b030a430f33057307e3059300200203053306e004f0054005030928ab030683082517167093057306a30443067304f306030553044\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是我用一些在线工具完成的。现在我需要使用nodejs做同样的事情。

\n\n

所以我的问题是 Unicode 类型是什么?如何将日语文本转换为 unicode?

\n

OPT*_*IME 5

\xe2\x80\xa2 .split("")\xe2\x80\x94 将字符串从每个字符分离到数组中 // ["\xe3\x81\x93", "\xe3\x81\xae", "O" ...]\n
\ xe2\x80\xa2 循环到数组中,并将map()每个字符替换为 charCode,转换为hex字符串。

\n\n

\r\n
\r\n
let str = "\xe3\x81\x93\xe3\x81\xaeOTP\xe3\x82\x92\xe4\xbd\xbf\xe7\x94\xa8\xe3\x81\x97\xe3\x81\xa6Quik\xe3\x83\x89\xe3\x83\xa9\xe3\x82\xa4\xe3\x83\x96\xe3\x81\xab\xe3\x83\xad\xe3\x82\xb0\xe3\x82\xa4\xe3\x83\xb3\xe3\x81\x97\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82 \xe3\x81\x93\xe3\x81\xaeOTP\xe3\x82\x92\xe8\xaa\xb0\xe3\x81\xa8\xe3\x82\x82\xe5\x85\xb1\xe6\x9c\x89\xe3\x81\x97\xe3\x81\xaa\xe3\x81\x84\xe3\x81\xa7\xe3\x81\x8f\xe3\x81\xa0\xe3\x81\x95\xe3\x81\x84";\r\n\r\nstr = str.split("").map( char => addZeros( char.charCodeAt(0).toString(16) ) ).join("");\r\n\r\nfunction addZeros(str){ return ("0000" + str).slice(-4) }\r\n\r\n\r\nconsole.log( str );\r\n\r\n// for comparison\r\nconsole.log( "3053306e004f0054005030924f7f752830573066005100750069006b30c930e930a430d6306b30ed30b030a430f33057307e3059300200203053306e004f0054005030928ab030683082517167093057306a30443067304f306030553044" )
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n

  • 您可以使用 `.replace()` 来消除将数据更改为数组,然后返回字符串的需要 (`const res = str.replace(/./g, c => c.codePointAt(0) .toString(16).padStart(4, '0'));`) (2认同)