3

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 become the proper SOH value(a blank unprintable character)

2 Answers 2

5

JS 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.org/api/buffer.html


EDIT: in the comment 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 common ASCII string use the common 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.

Sign up to request clarification or add additional context in comments.

8 Comments

FWIW you can convert between encodings in the browser too (as well as node) with iconv-lite.
Good to know! Thanks.
I am doing it in node, but I've never done anything with ASCII characters. It is seemingly impossible to get the SOH ascii character to show properly.
I've updated my answer. BTW what do you mean with "SHOW"? show where? in a HTML page?
The problem is when I send my messages to another server, their side sees '\u0001', I'm just hoping for it to be correct, it's been a frustrating week...
|
0

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.

3 Comments

Thanks, I'm kinda stuck with with Javascript and desperate to make it work. Is there an example of this? I've never actually dealt with ASCII characters and any help is greatly appreciated.
What are you trying to do?
I have a app(in nodejs on windows) that's connected to a C++ server, problem is when I send my message to the server, instead of it being the proper "SOH" ascii character, it shows either a smiley face or the unicode('\0001') which is not acceptable as a delimiter. I need it to be that blank space or whatever character it's supposed to be, and readable for the C++ application, Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.