Javascript将数字转换为utf-8

javascript convert number to utf-8

本文关键字:utf-8 转换 数字 Javascript      更新时间:2023-09-26

在C语言中,我可以这样做:

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

那么str将包含number的utf-8表示。是否有可能在javascript中做到这一点(没有0-padding)?

你想要String.fromCharCode

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

…与String.charCodeAt相反:

"A".charCodeAt(0); // 65