javascript 在javascript中将整数数组转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14028148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:32:39  来源:igfitidea点击:

Convert integer array to string at javascript

javascriptutf-8

提问by solomon_wzs

Here is php code:

这是php代码:

$arr=array(228,184,173,230,150,135,99,104,105,110,101,115,101);
$str='';
foreach ($arr as $i){
    $str.=chr($i);
}
print $str;

the output is: 中文chinese

输出是: 中文chinese

Here is javascript code:

这是javascript代码:

var arr=[228,184,173,230,150,135,99,104,105,110,101,115,101];
var str='';
for (i in arr){
    str+=String.fromCharCode(arr[i]);
}
console.log(str);

the output is: ??-?chinese

输出是: ??-?chinese

So how should I process the array at javascript?

那么我应该如何在javascript中处理数组呢?

回答by PleaseStand

JavaScript strings consist of UTF-16code units, yet the numbers in your array are the bytes of a UTF-8string. Here is one way to convert the string, which uses the decodeURIComponent()function:

JavaScript 字符串由UTF-16代码单元组成,但数组中的数字是UTF-8字符串的字节。这是转换字符串的一种方法,它使用decodeURIComponent()函数:

var i, str = '';

for (i = 0; i < arr.length; i++) {
    str += '%' + ('0' + arr[i].toString(16)).slice(-2);
}
str = decodeURIComponent(str);

Performing the UTF-8 to UTF-16 conversion in the conventional way is likely to be more efficient but would require more code.

以传统方式执行 UTF-8 到 UTF-16 转换可能更有效,但需要更多代码。

回答by ingrid

var arry = [3,5,7,9];
console.log(arry.map(String))

the result will be ['3','5','7','9']

结果将是 ['3','5','7','9']

var arry = ['3','5','7','9']
console.log(arry.map(Number))

the result will be [3,5,7,9]

结果将是 [3,5,7,9]

回答by smrtl

Another solution without decodeURIComponentfor characters up to 3 bytes (U+FFFF). The function presumes the string is valid UTF-8, not much error checking...

另一种不decodeURIComponent包含最多 3 个字节 (U+FFFF) 的字符的解决方案。该函数假定字符串是有效的 UTF-8,没有太多的错误检查......

function atos(arr) {
    for (var i=0, l=arr.length, s='', c; c = arr[i++];)
        s += String.fromCharCode(
            c > 0xdf && c < 0xf0 && i < l-1
                ? (c & 0xf) << 12 | (arr[i++] & 0x3f) << 6 | arr[i++] & 0x3f
            : c > 0x7f && i < l
                ? (c & 0x1f) << 6 | arr[i++] & 0x3f
            : c
        );

    return s
}

回答by Amit Khanna

Chinese charset has a different encoding in which one char is more than one byte long. When you do this

中文字符集有一种不同的编码,其中一个字符的长度超过一个字节。当你这样做时

for (i in arr){
    str+=String.fromCharCode(arr[i]);
}

You are converting each byte to a char(actually string) and adding it to a string str. What you need to do is, pack the bytes together.

您正在将每个字节转换为字符(实际上是字符串)并将其添加到字符串 str 中。您需要做的是,将字节打包在一起。

I changed your array to this and it worked for me:

我把你的数组改成了这个,它对我有用:

var arr=[20013,25991,99,104,105,110,101,115,101];

I got these codes from here.

我从这里得到这些代码。

you can also take a look at thisfor packing bytes to a string.

您还可以查看this将字节打包为字符串。

回答by Sancarn

Seems the best way these days is the following:

这些天似乎最好的方法如下:

function bufferToString(arr){
    arr.map(function(i){return String.fromCharCode(i)}).join("")
}