node.js - Javascript unicode to ASCII -


need convert unicode of soh value '\u0001' ascii. why not working?

var soh = string.fromcharcode(01);
returns '\u0001'
or when try

var soh = '\u0001' returns smiley face.
how can unicode become proper soh value(a blank unprintable character)

js has no ascii strings, they're intrinsically utf-16.

in browser you're out of luck. if you're coding node.js you're lucky!

you can use buffer transcode strings octets , manipulate binary data @ will. won't valid string out of buffer once you've messed it.

either way you'll have read more here:

https://mathiasbynens.be/notes/javascript-encoding

or here:

https://nodejs.org/api/buffer.html


edit: in comment use node.js, excerpt second link above.

const buf5 = buffer.from('test'); // creates buffer containing ascii bytes [74, 65, 73, 74]. 

to create soh character embedded in common ascii string use common escape sequence\x01 so:

const bufferwithsoh = buffer.from("string \x01 soh", "ascii"); 

this should it. can send bufferwithsoh content output stream such network, console or file stream.

node.js documentation guide on how use strings in buffer pretty well, second link above.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -