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

javascript convert number to utf-8 - Stack Overflow

programmeradmin3浏览0评论

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)?

Share Improve this question edited Jun 12, 2013 at 18:32 MiJyn asked Jun 12, 2013 at 18:27 MiJynMiJyn 5,8777 gold badges39 silver badges66 bronze badges 7
  • 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
 |  Show 2 more ments

1 Answer 1

Reset to default 15

You want String.fromCharCode...

String.fromCharCode(65);  // "A"

...which is the opposite of String.charCodeAt:

"A".charCodeAt(0); // 65
发布评论

评论列表(0)

  1. 暂无评论