admin管理员组文章数量:1355748
Need to convert the unicode of the SOH value '\u0001' to Ascii. Why is this not working?
var soh = String.fromCharCode(01);
It returns '\u0001'
Or when I try
var soh = '\u0001'
It returns a smiley face.
How can I get the unicode to bee the proper SOH value(a blank unprintable character)
Need to convert the unicode of the SOH value '\u0001' to Ascii. Why is this not working?
var soh = String.fromCharCode(01);
It returns '\u0001'
Or when I try
var soh = '\u0001'
It returns a smiley face.
How can I get the unicode to bee the proper SOH value(a blank unprintable character)
2 Answers
Reset to default 5JS has no ASCII strings, they're intrinsically UTF-16.
In a browser you're out of luck. If you're coding for node.js you're lucky!
You can use a buffer to transcode strings into octets and then manipulate the binary data at will. But you won't get necessarily a valid string back out of the buffer once you've messed with it.
Either way you'll have to read more about it here:
https://mathiasbynens.be/notes/javascript-encoding
or here:
https://nodejs/api/buffer.html
EDIT: in the ment you say you use node.js, so this is an excerpt from the second link above.
const buf5 = Buffer.from('test');
// Creates a Buffer containing ASCII bytes [74, 65, 73, 74].
To create the SOH character embedded in a mon ASCII string use the mon escape sequence\x01
like so:
const bufferWithSOH = Buffer.from("string with \x01 SOH", "ascii");
This should do it. You can then send the bufferWithSOH
content to an output stream such as a network, console or file stream.
Node.js documentation will guide you on how to use strings in a Buffer
pretty well, just look up the second link above.
To ascii would be would be an array of bytes: 0x00 0x01
You would need to extract the unicode code point after the \u
and call parseInt
, then extract the bytes from the Number. JavaScript might not be the best language for this.
本文标签: nodejsJavascript unicode to ASCIIStack Overflow
版权声明:本文标题:node.js - Javascript unicode to ASCII - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744018547a2576784.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论