如何在C+中从bytearray提取字节时交换64位整数+;? 我想从我的C++代码中从下面的代码> ByTeDATAs/COD>中读出几个字节。byteData中的实际值是一个二进制blob字节数组,采用BIG-ENDIAN字节顺序格式。因此,我不能简单地将字节数组“转换”为字符串
如何在C+中从bytearray提取字节时交换64位整数+;? 我想从我的C++代码中从下面的代码> ByTeDATAs/COD>中读出几个字节。byteData中的实际值是一个二进制blob字节数组,采用BIG-ENDIAN字节顺序格式。因此,我不能简单地将字节数组“转换”为字符串,c++,bytearray,endianness,C++,Bytearray,Endianness,byteData字节数组由以下三部分组成- First is `schemaId` which is of two bytes (short datatype in Java) Second is `lastModifiedDate` which is of eight bytes (long datatype in Java) Third is the length of actual `byteArray` within `byteData` which we need from `byte
byteData
字节数组由以下三部分组成-
First is `schemaId` which is of two bytes (short datatype in Java)
Second is `lastModifiedDate` which is of eight bytes (long datatype in Java)
Third is the length of actual `byteArray` within `byteData` which we need from `byteData`.
Fourth is the actual value of that `byteArray` in `byteData`.
现在我试图从C++中的代码< ByTeDATAs/COD>中提取上述特定信息。不知何故,我能够提取出schemaId
,并且我得到的值也是正确的。。现在我不知道如何从中提取其他东西
uint16_t schemaId;
uint64_t lastModifiedDate;
uint16_t attributeLength; // or it should be uint32_t?
const char* actual_binary_value;
while (result.next()) {
for (size_t i = 0; i < result.column_count(); ++i) {
cql::cql_byte_t* byteData = NULL;
cql::cql_int_t size = 0;
result.get_data(i, &byteData, size);
// I cannot just "cast" the byte array into a String
// value = reinterpret_cast<char*>(byteData);
// this works fine
schemaId = ntohs(*reinterpret_cast<uint16_t*>(byteData));
// now how to retrieve lastModifiedDate, length of binary value and actual_binary_value from byteData?
// the below doesn't works..
lastModifiedDate = be64toh(*reinterpret_cast<uint64_t*>(data));
// And how to extract other things as well?
}
// this prints out `9223090561897746107` but it should print out `1289811105109`
cout<< lastModifiedDate <<endl;
// And print out other things..
}
谁能帮助我在C++代码中做什么错误,为什么我不能从它和其他字段中正确地提取<代码> LSTMeXDFIDENDATE <代码>?据我所知,lastModifiedDate
是64位整数,所以这里有没有办法换掉64位整数?或者其他更好的转换方法
P>简单的说,我想从C++中的那个字节数组中提取<代码> StaseAudio/<代码> >代码> LSTMeXDIDENDATE < /COD>,<代码> avRObIALValuy。
我能够提取
schemaId
,但是我现在被其他事情卡住了…您的方法看起来不错,我只看到了两个可能的问题
data
转换为uint64\u t
。确保您实际上正在通过数据缓冲区并转换正确的数据int index=0;
schemaId = ntohs(*reinterpret_cast<uint16_t*>(&byteData[index]));
index += 2;
lastModifiedDate = be64toh(*reinterpret_cast<uint64_t*>(&byteData[index]));
index += 8;
attributeLength = ntohl(*reinterpret_cast<uint32_t*>(&byteData[index]));
index += 4;
actual_binary_data = (const char *)&byteData[index];
int索引=0;
schemaId=ntohs(*重新解释铸件(&byteData[index]);
指数+=2;
lastModifiedDate=be64toh(*重新解释铸件(&byteData[index]);
指数+=8;
attributeLength=ntohl(*重新解释铸件和byteData[索引]);
指数+=4;
实际二进制数据=(常量字符*)&字节数据[索引];
可能重复@JonathanPotter:在那个问题上,我问了schemaId和其他一些我同意的问题,但有人建议我寻找如何交换64位整数以从中提取lastModifiedDate,我做了一些研究,但我无法弄清楚。。。这就是为什么在这里作为一个新问题发布…谢谢你的建议。。。与你的第一点相对应,我做错了什么吗?我们不能手动交换位,而不是使用一些预定义的函数?用示例代码更新了答案。Re:手动交换,您可能应该避免它,并尽可能使用库函数。代码将更便于移植。谢谢您的建议。。我两天以来一直在解决这个问题。。因为我的背景是Java,所以我发现要弄明白所有这些事情有点困难。。谢谢你的帮助。。但是在编译了上面的代码之后,我得到了一个异常-错误:从类型cql::cql_byte_t*{aka unsigned char*}到类型uint16_t*{aka short unsigned int*}的静态转换无效
知道怎么了吗?抱歉,转换类型错误。您可以使用reinterpret_cast,也可以使用隐式cast。非常感谢Chris的帮助。。感谢你在这方面的帮助。。现在很好用。。我想知道的最后一件事是,在attributeLength
中,您正在使用uint32\u t*
,我不确定我们是应该使用uint32\u t*
还是uint16\u t*
。。这意味着为什么您决定使用uint32\u t*
。。抱歉问了个愚蠢的问题。。。有什么想法吗?
int index=0;
schemaId = ntohs(*reinterpret_cast<uint16_t*>(&byteData[index]));
index += 2;
lastModifiedDate = be64toh(*reinterpret_cast<uint64_t*>(&byteData[index]));
index += 8;
attributeLength = ntohl(*reinterpret_cast<uint32_t*>(&byteData[index]));
index += 4;
actual_binary_data = (const char *)&byteData[index];