使用Uint8Array double转换JS值将其转换为float

时间:2016-07-21 12:53:13

标签: javascript binary uint8array

我发送使用HTML5网络套接字对象移动,然后我使用Uint8Array转换此值。 问题是移动对象时获得的值是双倍的 在转换后获得浮动值。 这是一个例子:      初始值:

player_x:633.8823529411765
player_y:412.235294117647



_stringConvertToUint8Array:function (strData) {  
    // View the byte buffer as an array of 8-bit unsigned integers
    var arrData = new Uint8Array(strData.length);
    for (var i=0;i<strData.length;++i) {
        arrData[i] = strData.charCodeAt(i);
    }
    // Log the binary array
    cc.log("SEND ARRAY BUFFER: " + arrData.buffer);
    return arrData;
},
sendMassage: function(reqJson)
{
    if (this.m_wsiSendBinary.readyState == WebSocket.OPEN)
    {

        var buf = Encode(reqJson);
        var binary = this._stringConvertToUint8Array(buf);
        this.m_wsiSendBinary.send(binary.buffer);
    }
    else
    {
        var warningStr = "send binary websocket instance wasn't ready...";

        }
    },

打印结果

SEND ARRAY BUFFER: [object ArrayBuffer]
Websockts.js?_t=1469105183221:30 {   
    "player_y": 412.235294,
    "player_x": 633.882353
}

0 个答案:

没有答案