How can I convert string to unicode?

心不动则不痛 提交于 2019-12-13 08:57:01

问题


I have my one text

Japanese:  このOTPを使用してQuikドライブにログインします。 このOTPを誰とも共有しないでください

I have its unicode conversion

Unicode:  3053306e004f0054005030924f7f752830573066005100750069006b30c930e930a430d6306b30ed30b030a430f33057307e3059300200203053306e004f0054005030928ab030683082517167093057306a30443067304f306030553044

This is I have done with some online tool. Now I need to do same thing using nodejs.

So my question is what is that type of Unicode? How can I convert it my Japanese text to unicode?


回答1:


.split("") — separate string into array from each character // ["こ", "の", "O" ...]
• loop into array with map() and replace each character with charCode, converted to hex string.

let str = "このOTPを使用してQuikドライブにログインします。 このOTPを誰とも共有しないでください";

str = str.split("").map( char => addZeros( char.charCodeAt(0).toString(16) ) ).join("");

function addZeros(str){ return ("0000" + str).slice(-4) }


console.log( str );

// for comparison
console.log( "3053306e004f0054005030924f7f752830573066005100750069006b30c930e930a430d6306b30ed30b030a430f33057307e3059300200203053306e004f0054005030928ab030683082517167093057306a30443067304f306030553044" )


来源:https://stackoverflow.com/questions/59026205/how-can-i-convert-string-to-unicode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!