在C#中运行
UnipreEncoding encoding = new UnipreEncoding();byte[] bytes = encoding.GetBytes("Hello");将创建一个数组
72,0,101,0,108,0,108,0,111,0
对于代码大于255的字符,它将如下所示
如果您希望在Javascript中实现非常相似的行为,则可以执行此操作(v2是更强大的解决方案,而原始版本仅适用于0x00〜0xff)
var str = "Hello竜";var bytes = []; // char presvar bytesv2 = []; // char presfor (var i = 0; i < str.length; ++i) { var pre = str.charCodeAt(i); bytes = bytes.concat([pre]); bytesv2 = bytesv2.concat([pre & 0xff, pre / 256 >>> 0]);}// 72, 101, 108, 108, 111, 31452console.log('bytes', bytes.join(', '));// 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 220, 122console.log('bytesv2', bytesv2.join(', '));


