简体   繁体   English

使用Buffer对象解码base64输入

[英]Decoding base64 input with a Buffer object

I have a client interface that sends binary data encoded in base64. 我有一个客户端接口,该接口发送以base64编码的二进制数据。 The data is a 29 byte custom formatted payload of bytes that describes an event. 数据是描述事件的29字节自定义格式的字节有效载荷。 The 29 bytes is made up of a number of fields; 这29个字节由许多字段组成。 each with unique lengths. 每个都有独特的长度。

I need the server to decode that so I can extract the fields. 我需要服务器对其进行解码,以便提取字段。 I've tried using the Buffer object as I did with another use case where the data was passed in hex format , without success. 我已经尝试过使用Buffer对象,就像在另一个用例中那样, 数据以十六进制格式传递而没有成功。

buff = new Buffer('AR0AAAEKCgsLDAwAAATSAAAADsgAAAAAAAAAzMQ=', 'base64');

// track the current position
// ... get out to the data portion of the message
var position = 3;

// event type
var event_type = buff.slice(position,(position+3)).toString('utf8');
position += 3;
console.log('... event type: ' + event_type + ' /');

// address
var addr = buff.slice(position,(position+3)).toString('utf8');
position += 3;
console.log('... addr: ' + addr + ' /');

The .toString('utf8') is likely the root cause. .toString('utf8')可能是根本原因。 How can I get to a String that represents the desired bytes in these slice() calls? 如何获得一个字符串,该字符串表示这些slice()调用中的所需字节?

I'm not storying binary data on the server as one might normally do with base64 encoding. 我不是在讲述服务器上的二进制数据,就像通常使用base64编码所做的那样。 So should I be going from base64 to some other encoding to access the individual bytes? 所以我应该从base64转到其他编码来访问各个字节吗?

I've used the same code on a known text string that is encoded with base64 to verify the basic logic. 我在用base64编码的已知文本字符串上使用了相同的代码来验证基本逻辑。 But when the source data is binary before being encoded, the console statements don't print anything. 但是,如果源数据在被编码之前是二进制的,则控制台语句不会输出任何内容。 Can I go from binary to base64 to strings? 我可以从二进制到base64再到字符串吗?

The data does not seem to have valid ASCII or UTF-8 strings, the event_type and addr is some binary data. 数据似乎没有有效的ASCII或UTF-8字符串,event_type和addr是一些二进制数据。 Use hexadecimal presentation and print that. 使用十六进制表示法并进行打印。

var event_type = buff.slice(3,6).toString('hex');  => '00010a'

The problem with this implementation is the use of slice() to dissect the byte stream. 此实现的问题是使用slice()解析字节流。 The parameters to slice() are indexes - not byte offsets. slice()的参数是索引-不是字节偏移量。 This seems like a pretty big flaw. 这似乎是一个很大的缺陷。 I'm not sure why anyone would do that when interacting with a buffer. 我不确定为什么有人在与缓冲区交互时会这样做。

Regardless, the solution is to access the buff as an array. 无论如何,解决方案是将buff作为数组进行访问。

// event type
var event_type = buff[position].toString('utf8');
position += 1;
console.log('... event type: ' + event_type + ' /');

// address
var addr = buff[position].toString('utf8');
position += 1;
console.log('... addr: ' + addr + ' /');

If you want to concatenate multiple bytes (the original example has 3 byte lengths), you have to loop over the array indexes. 如果要串联多个字节(原始示例的长度为3个字节),则必须遍历数组索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM