最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I convert string to unicode? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Nov 25, 2019 at 6:18 ProferProfer 64310 gold badges47 silver badges97 bronze badges 5
  • 2 "このOTPを使用してQuikドライブにログインします。 このOTPを誰とも共有しないでください".split("").map(codepoint => codepoint.codePointAt(0).toString(16)).join(""); and yes, in this case, split is exactly what you want, because it splits by codepoints, and not graphemes. – ASDFGerte Commented Nov 25, 2019 at 6:21
  • It's the unicode code points of your string in base16 – Nick Parsons Commented Nov 25, 2019 at 6:23
  • @ASDFGerte Well You can answer it as well – Profer Commented Nov 25, 2019 at 6:32
  • There already is an adequate answer, saving me the effort to write one, thanks :) – ASDFGerte Commented Nov 25, 2019 at 6:42
  • What do you want to do and why? To convert Unicode characters to numbers you have to use an encoding method like UTF-8, UTF-16, UTF-32 etc. Number values and number of bytes per character differ between encoding schemes so knowing the encoding used is needed to reverse the process. So what is the design objective here? – traktor Commented Nov 25, 2019 at 6:46
Add a ment  | 

1 Answer 1

Reset to default 6

.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 parison
console.log( "3053306e004f0054005030924f7f752830573066005100750069006b30c930e930a430d6306b30ed30b030a430f33057307e3059300200203053306e004f0054005030928ab030683082517167093057306a30443067304f306030553044" )

发布评论

评论列表(0)

  1. 暂无评论