Tag: 字节

从二进制string读取可变长度位

即时通讯新的javascript和node.js,我有一个base64编码的数据string,我需要parsing几个不同长度的值。 我想我会开始使用Buffer对象来读取b64string,但从那里我完全丢失。 数据是一系列无符号整数,格式与此类似:头文件: 8 bits – uint 3 bits – uint 2 bits – uint 3 bits – unused padding 6 bits – uint 之后,有23位或13位数据长度的循环部分,每个部分都有一些我需要提取的字段。 一个23位段的例子: 3 bit – uint 10 bit – uint 10 bit – uint 我的问题是,什么是采取任意位数,并把结果值在一个单独的uint最好的方法是什么? 请注意,其中一些值是多字节(> 8位),所以我不能逐字节的字节。 我很抱歉,如果我的解释是模糊的,但希望它就足够了。

有效负载字节与实际字节不同

我有一个问题,当用户发送图像数据,它保存在服务器上的损坏。 所以,我基本上有这样的设置: – api . index.js – methods . users.js (我已经删除了与问题无关的文件) 在API文件之外,有一个server.js ,每当访问api.example.com时都会启动它。 (基本上像一个VirtualHost) 话虽如此。 这是问题: ./api/index.js (问题点) // All this essentiall does is get the get the whole payload as a buffer. req.on("data", function(chunk){ // Potential problem area. req.api.payload = ((req.api.payload) ? (Buffer.concat([req.api.payload, chunk])) : (chunk)); }); req.on("end", function(){ // Check if we're on […]

JS与Python的字节数组编码

我试图将字节数组转换为string,然后通过套接字发送到远程服务器。 我已经成功地在Python原型的代码,并试图将其迁移到Javascript 。 由于某种原因,这两种语言之间的最后一个字符有差异。 Python代码 def make_checksum(data): num = 0x00 for num2 in data: num = (num + num2) & 0xFF return num data = [0x56, 0x54, 0x55, 0x3E, 0x28, 0x00, 0x08, 0x00, 0x03, 0x01, 0x46, 0x00, 0x00, 0x00, 0xC0] message = bytearray(data + [make_checksum(data)]) 使用Javascript function checksum(data) { let res = 0x00 for (let […]

Node.js,protobuffer,buffer.lentgh ..如何发送结构化的缓冲区/消息槽tcp?

我写了一个节点/ ssjs程序,使: Tcp连接到数据服务器(Apache MIMA),使用TLS模块。 (好) 通过protobuffer模块对消息进行编码/解码(序列化/反序列化)。(OK) 将序列化的消息发送到服务器并获得响应。 (不好)。 服务器的手册types: Structure of message: [ Length | Header length | Header (| Body length |Body) ] Length – message length = fixed size (4 bytes). Note that this is only the size of following message (not the prefix itself); Header length – fixed size (4 bytes); Header […]

如何在node.js中获取string的字节?

我想读取文件的字节大小。 我有这个 var path = 'training_data/dat1.txt'; var fs = require("fs"); //Load the filesystem module var stats = fs.statSync(path); var fileSizeInBytes = stats["size"]; var accSize = 0; var lineReader = require('readline').createInterface({ input: fs.createReadStream(path) }); lineReader.on('line', function (line) { accSize += Buffer.byteLength(line, 'utf8'); console.log(accSize + "/" + fileSizeInBytes); }); lineReader.on('close', function() { console.log('completed!'); }); 但它不打印出正确的文件大小。 7/166 16/166 […]

JavaScript:读取8个字节到64位整数

我有一个包含八个字节的缓冲区对象。 这八个字节现在应该被解释为64位整数。 目前我使用以下algorithm: var int = buff[0]; for (var i = 1; i < buff.length; i++) { int += (buff[i] * Math.pow(2, 8 * i)); } console.log(int); 这工作,但我相信有更好的方法(也许使用Uint64Array)。 不幸的是,我无法find一个Uint16Array如何帮助我。 问候 更新: // puts two 32bit integers to one 64bit integer var bufInt = (buf.readUInt32BE(0) << 8) + buf.readUInt32BE(4);

nodejs将64位无符号整数写入缓冲区

我想以64字节(8字节)的大整数存储一个nodejs缓冲区对象的big endian格式。 关于这个任务的问题是nodejs缓冲区只支持最大写32位整数(使用buf.write32UInt32BE(value,offset))。 所以我虽然为什么不能把64位整数分开? var buf = new Buffer(8); buf.fill(0) // clear all bytes of the buffer console.log(buf); // outputs <Buffer 00 00 00 00 00 00 00 00> var int = 0xffff; // as dezimal: 65535 buf.write32UInt32BE(0xff, 4); // right the first part of the int console.log(buf); // outputs <Buffer 00 00 00 00 […]

将node.js中的字节数组发送到服务器

在连接到Debian上的服务器时,我无法想象发送字节数组。 有没有其他的发送string通过client.write() ? 我尝试client.write(new Buffer("something"))但这给Invalid data错误。 这是我的代码: var net = require('net'); function ModernBuffer(buffer) { // custom buffer class this.buffer = new ArrayBuffer(buffer.length); this.byteLength = this.buffer.byteLength; console.log('ModernBuffer.ByteLength: ' + this.byteLength); var Uint16View = new Uint16Array(this.buffer, 0, 1); for (var i=0; i<Uint16View.length; i++) { Uint16View[i] = buffer[i]; console.log("Entry " + i + ": " + Uint16View[i]); } […]