简体   繁体   English

从Javascript中的UTF代码创建UTF8字符串

[英]Create UTF8 String from UTF Codes in Javascript

I have the byte representation of UTF8, eg 我有UTF8的字节表示,例如

195, 156 for "Ü" (capital U Umlaut)

I need to create a string for display in JavaScript out of these numbers - everything I tried failed. 我需要从这些数字中创建一个字符串以在JavaScript中显示-我尝试的所有操作均失败。 No methode I found recognizes "195" as a UTF leading byte but gave mit "Ã". 我发现没有方法可以将“ 195”识别为UTF前导字节,但给了mit“Ô。

So how do I get a string to display from my stream of UTF8 bytes? 那么,如何从UTF8字节流中显示字符串呢?

You're working with decimal representations of the single byte components of the characters. 您正在使用字符的单字节组成部分的十进制表示形式。 For the example given, you have 195, 156. First, you have to converting to base 16's C3, 9C. 对于给定的示例,您有195、156。首先,必须转换为以16为基数的C3、9C。 From there you can use javascript's decodeURIComponent function. 从那里,您可以使用javascript的encodeURIComponent函数。

console.log(decodeURIComponent(`%${(195).toString(16)}%${(156).toString(16)}`));

If you're doing this with a lot of characters, you probably want to find a library that implements string encoding / decoding. 如果您要处理很多字符,则可能需要找到一个实现字符串编码/解码的库。 For example, node's Buffer objects do this internally. 例如,节点的Buffer对象在内部执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM