use*_*762 1 javascript websocket node.js socket.io
我已经在这方面做了一点,我不熟悉JS编程.我正在使用JS,HTML5,node和socket.io制作游戏.我正在研究协议,我正在发送十六进制的服务器字符串.
字符串的示例是:00010203040506070809
我很难将其转换为:0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09
我打算做的是根据数据包获取这些自定义数据包并在我的服务器上切换.例如:
BYTE HEADER | + Packet
0x00 | 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09
Run Code Online (Sandbox Code Playgroud)
然后我调用:parsepacket(header,data,len);
function parsepacket(header, data, len){
switch(header)
{
case '0x00': // not hexed
console.log('The client wants to connect');
// Do some stuff to connect
break;
case '0x01':
console.log('0x01');
break;
case '0x02':
console.log('0x02!');
break;
}
};
Run Code Online (Sandbox Code Playgroud)
有谁知道如何做到这一点?
我不确定这是你所追求的,但你可以将字符串转换为十六进制值数组,如下所示:
var str = "00010203040506070809",
a = [];
for (var i = 0; i < str.length; i += 2) {
a.push("0x" + str.substr(i, 2));
}
console.log(a); // prints the array
console.log(a.join(" ")); // turn the array into a string of hex values
?console.log(parseInt(a[1], 16));? // parse a particular hex number to a decimal value
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11078 次 |
| 最近记录: |