Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将字符数组解释为多个连接的数据类型_C++_Arrays_Casting_Char_Byte - Fatal编程技术网

C++ 将字符数组解释为多个连接的数据类型

C++ 将字符数组解释为多个连接的数据类型,c++,arrays,casting,char,byte,C++,Arrays,Casting,Char,Byte,我通过套接字接收字节流(或C++中的字符)。现在我要翻译它们。我知道哪些数据类型隐藏在字节后面。我的信息似乎是这样的: value1 --> char (1 byte) value2 --> long (8 bytes) value3 --> short (2 bytes) ... 我怎样才能有效地完成口译 //编辑:如果不起作用,字节不描述字符,而是描述整数。 我想用memcpy和atoi(到目前为止还没有测试): 我假设您正在尝试将数据作为原始字节流发送,并

我通过套接字接收字节流(或C++中的字符)。现在我要翻译它们。我知道哪些数据类型隐藏在字节后面。我的信息似乎是这样的:

value1   --> char (1 byte)
value2   --> long (8 bytes)
value3   --> short (2 bytes)
... 
我怎样才能有效地完成口译

//编辑:如果不起作用,字节不描述字符,而是描述整数。

我想用memcpy和atoi(到目前为止还没有测试):


我假设您正在尝试将数据作为原始字节流发送,并且您已经得到保证,发送方和接收方使用相同的浮点格式和相同的尾数

但是您的压缩数据流显然违反了发送方和接收方的对齐规则,因此我们不能执行类似
value2=*(long*)数据的操作

因此,尝试使用
parsePackedStruct(数据,&value1,&value2,&value3)

如果合理的实施可以是:

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));
}
这要求您的发送方和接收方对于
char/short/long
的大小完全相同,这对我来说太严格了,因为
long
的大小在不同平台之间的变化要比其他算术类型多得多。出于同样的原因,我也会避免使用
bool
,而使用
uint8\u t


<> P> >考虑C99整数类型,如<代码> It32→t>代码>

假设发送方和接收方与发送/接收的原始数据完全同步,您可以这样做:

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


好吧,我知道怎么做了。关键字是位移位:

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);
}
...
我解释:

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

知道臭虫在哪里吗

您是否考虑过使用std::stringstream及其操作符>>来提取数据?char*中的数据不是字符,因此我认为stringstream不起作用。
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);
}
...
152  --> 00000000 00000000 00000000 00000000 00000000 00000000 00000000 10011000
-104  --> 11111111 11111111 11111111 11111111 11111111 11111111 11111111 10011000