In C, I'd be able to do something like this:
short number = 20693; // Create the number
unsigned char* str = malloc(3); // Create the string
memcpy(str, &number, 2); // Copy the number to the string
str[2] = 0; // 0-pad it
And then str
would contain the utf-8 representation of number
. Is it possible to do this in javascript (without the 0-padding)?
In C, I'd be able to do something like this:
short number = 20693; // Create the number
unsigned char* str = malloc(3); // Create the string
memcpy(str, &number, 2); // Copy the number to the string
str[2] = 0; // 0-pad it
And then str
would contain the utf-8 representation of number
. Is it possible to do this in javascript (without the 0-padding)?
- 2 Possible dup: stackoverflow./questions/9939760/… – tymeJV Commented Jun 12, 2013 at 18:28
- Obviously it's possible, but your C example is hilariously irrelevant to a JavaScript solution. – user229044 ♦ Commented Jun 12, 2013 at 18:29
- @tymeJV, nope, that one wants to convert a number into a binary number (like 25 would bee 11001), and I want the number to be, well, encoded in binary (like 65 would bee "A") – MiJyn Commented Jun 12, 2013 at 18:29
- @MiJyn Err, maybe take out the reference to "binary" then. You're looking for the ASCII number for a given character, this has nothing to do with binary. – user229044 ♦ Commented Jun 12, 2013 at 18:31
- 2 I don't believe this is a duplicate. they are two pletely different questions. Which the answers are both different as you can see. – mjwrazor Commented May 9, 2017 at 19:49
1 Answer
Reset to default 15You want String.fromCharCode
...
String.fromCharCode(65); // "A"
...which is the opposite of String.charCodeAt
:
"A".charCodeAt(0); // 65