如何将arraybuffer转换为字符串 [英] how to convert arraybuffer to string

查看:705
本文介绍了如何将arraybuffer转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在node.js上编写了一个简单的TCP服务器,以将一些数据发送到Chrome应用.在chrome应用程序中,当我获取数据时,我使用下面的函数将其转换为字符串,但出现异常"Uint16Array的字节长度应为2的倍数"

I have written a simple TCP server on node.js to send some data to a Chrome app. In the chrome app, when I get the data, I convert that to string using below function, I get an exception "byte length of Uint16Array should be a multiple of 2"

String.fromCharCode.apply(null, new Uint16Array(buffer))

我找不到有关可能导致此问题以及如何解决此问题的任何信息.对此的任何指示都将受到高度赞赏.

I could not find any information about what could be causing this and how to fix this. Any pointers on this is highly appreciated.

以下是node.js服务器中用于将数据发送到客户端的代码:

Below is the code in node.js server for sending the data to client:

socket.on('data', function(data) {

    console.log('DATA ' + socket.remoteAddress + ': ' + data);
    // Write the data back to the socket, 
    //   the client will receive it as data from the server
    var r= socket.write('from server\r\n');

});

以下是chrome应用程序中的代码:

Below is the code from chrome app:

  chrome.sockets.tcp.onReceive.addListener(function (info) {
            console.log('onListener registered');
            if (info.socketId != socketid)
                return;
            else {
                try {

                   data = ab2str(info.data);
                    console.log(data);
                }
                catch (e) {
                    console.log(e);
                }

            }
            // info.data is an arrayBuffer.
        });

 function ab2str(buf) {
    return String.fromCharCode.apply(null, new Uint16Array(buf));
}

推荐答案

您可能会看到此问题,因为您的应用程序在套接字上收到了奇数个字节,但是您试图创建一个2的数组字节宽的项目(因为这是Uint16Array的内容)

You're probably seeing this problem because your app has received an odd number of bytes on the socket, but you're trying to create an array of 2-byte-wide items out of it (because that's what fits into a Uint16Array)

如果您的应用程序通过网络(5个字节)接收到字符串"Hello",则可以将其强制转换为Uint8Array,它将看起来像这样:

If your app receives the string "Hello" over the network (5 bytes), then you can cast that to a Uint8Array, and it will look like this:

Item:        0   1   2   3   4
Char:        H   e   l   l   o
Uint8 Value: 72  101 108 108 111

将其广播到Uint16Array,尽管会尝试这样做:

casting it to an Uint16Array, though will try to do this:

Item   0     1     2
Chars  He    ll    o?
IntVal 25928 27756 ?????

没有第6个字节可以使用,它无法构造数组,因此会出现异常.

Without a 6th byte to work with, it can't construct the array, and so you get an exception.

仅当您期望套接字上的UCS-2字符串数据时,对数据使用Uint16Array才有意义.如果接收的是纯ASCII数据,则要将其转换为Uint8Array,然后在其上映射String.fromCharCode.如果是其他内容(例如UTF-8),则必须进行其他一些转换.

Using a Uint16Array for the data only makes sense if you are expecting UCS-2 string data on the socket. If you are receiving plain ASCII data, then you want to cast that to a Uint8Array instead, and map String.fromCharCode on that. If it's something else, such as UTF-8, then you'll have to do some other conversion.

尽管如此,套接字层始终可以自由地以任意长度的块向您发送数据.您的应用程序必须处理奇数大小的数据,并保存所有您无法立即处理的剩余数据,以便在收到下一个数据块时可以使用它.

No matter what, though, the socket layer is always free to send you data in chunks of any length. Your app will have to deal with odd sizes, and save any remainder that you can't deal with right away, so that you can use it when you receive the next chunk of data.

这篇关于如何将arraybuffer转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆