简体   繁体   English

将Char数组解释为多个串联的数据类型

[英]Interpreting Char-Array to Multiple concatenated Datatypes

I receive via Socket a stream of bytes (or chars in C++). 我通过套接字收到字节流(或C ++中的字符)。 Now I want to interprete them. 现在我要解释它们。 I know which datatypes are hidden behind the bytes. 我知道哪些数据类型隐藏在字节后面。 My message looks somehow like that: 我的消息看起来像这样:

value1   --> char (1 byte)
value2   --> long (8 bytes)
value3   --> short (2 bytes)
... 

How can I achieve the interpretation efficiently? 如何有效地进行解释?

//Edit: that don't work, the bytes don't describe characters but integers. //编辑:不起作用,字节不是描述字符,而是整数。

I thought about doing is with memcpy and atoi (not tested so far): 我考虑过要使用memcpy和atoi(到目前为止尚未测试):

char value1 = *charPtr;
charPtr++;

char value2[8]="";
std::memcpy(charPtr,value2,8);
long v2 = atoi(value2);
charPtr+=8;

char value3[2]="";
std::memcpy(charPtr,value3,2);
short v3 = atoi(value3);
charPtr+=2;

I assume that you are trying to send data as raw byte stream, and you've been assured that sender and receiver uses the same floating point format and same endiness . 我假设您尝试将数据作为原始字节流发送,并且已经确定发送方和接收方使用相同的浮点格式相同的endiness

But your packed data stream apparently violates alignment rules on both sender and receiver sides, so we can not do something like value2 = *(long *)data; 但是您打包的数据流显然违反了发送方和接收方的对齐规则,因此我们无法执行类似于value2 = *(long *)data;

So, try using parsePackedStruct(data, &value1, &value2, &value3); 因此,尝试使用parsePackedStruct(data, &value1, &value2, &value3);

where a reasonable implementation can be: 合理的实现可以是:

void parsePackedStruct(char *data, char *pValue1, long *pValue2, short *pValue3)
{
    memcpy(pValue1, data, sizeof(*pValue1));
    data = data + sizeof(*pLalue1);
    memcpy(pValue2, data, sizeof(*pValue2));
    data = data + sizeof(*pLalue2);
    memcpy(pValue3, data, sizeof(*pValue3));
}

This requires your sender and receiver have strictly the same size for char/short/long , which seems too strict to me because size of long varies from platform to platform much more often than other arithmetic types. 这要求您的发送方和接收方的char/short/long大小严格相同,这对我来说似乎太严格了,因为long大小因平台而异,比其他算术类型更频繁。 For the same reason, I'd also avoid bool and use uint8_t . 出于同样的原因,我也避免使用bool并使用uint8_t

So consider C99 integer types like int32_t . 因此,请考虑使用int32_t类的C99整数类型。

Assuming sender and receiver are in perfect sync as to the raw data sent/received, you could do something like this: 假设发送方和接收方在发送/接收的原始数据方面完全同步,则可以执行以下操作:

#pragma pack(push, 1)
struct MyPackedData {
  char c;
  long l;
  short s;
};
#pragma pack(pop)

... ...

MyPackedData parsedData;
memcpy(&parsedData, charPtr, sizeof(parsedData));

Okey, I figured out how to do this. Okey,我知道了该怎么做。 The keyword is bit shifting: 关键字是移位:

long readAndSkipLong(char*& b)
{
    unsigned long ret = (b[0] << 56) | (b[1] << 48) | (b[2] << 40) | (b[3]<<32) | (b[4] << 24) | (b[5] << 16) | (b[6] << 8) | (b[7]);
    b+=8;
    return ret;
}

int readAndSkipInt(char*& b)
{
    int32_t ret = (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
    b+=4;
    return ret;
}

short readAndSkipShort(char*& b) {
    short ret =  (b[0] << 8) | (b[1]);
    b+=2;
    return ret;
}

...
while (readChar(it)!='#') // stop at the terminating char
{
        readAndSkipShort(it);
        readAndSkipInt(it);
}
...

Nevertheless my shifting for long and int seems not to be right. 然而,我转移了很长时间并且很聪明,这似乎是不对的。 For the intended value 对于预期值

152  --> 00000000 00000000 00000000 00000000 00000000 00000000 00000000 10011000

I interpret: 我解释:

-104  --> 11111111 11111111 11111111 11111111 11111111 11111111 11111111 10011000 

any idea where the bug is? 知道错误在哪里吗?

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

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