gpt4 book ai didi

c++ - 如何在 C++ 中从 bytearray 中提取字节时交换 64 位整数?

转载 作者:行者123 更新时间:2023-11-28 07:21:29 24 4
gpt4 key购买 nike

我正在尝试从 byteData 中读取几个字节,如下面在我的 C++ 代码中提到的那样。 byteData 中的实际值是 BIG-ENDIAN 字节顺序格式的二进制 blob 字节数组。所以我不能简单地将字节数组“转换”为字符串..

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++ 中的 byteData 中提取上述特定信息......我以某种方式能够提取 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..
}

如果有人需要查看我的 java 代码,那么这是我的 java 代码 -

    byte[] avroBinaryValue = text.getBytes();

long lastModifiedDate = 1289811105109L;
short schemaId = 32767;

int size = 2 + 8 + 4 + avroBinaryValue.length; // short is 2 bytes, long 8 and int 4

ByteBuffer bbuf = ByteBuffer.allocate(size);
bbuf.order(ByteOrder.BIG_ENDIAN);

bbuf.putShort(schemaId);
bbuf.putLong(lastModifiedDate);
bbuf.putInt(avroBinaryValue.length);
bbuf.put(avroBinaryValue);

// merge everything into one bytearray.
byte[] bytesToStore = bbuf.array();

Hex.encodeHexString(bytesToStore)

任何人都可以帮助我在我的 C++ 代码中做错什么以及为什么我无法从它和其他字段中正确提取 lastModifiedDate 吗?据我了解,lastModifiedDate 是 64 位整数,那么这里有什么方法可以换出 64 位整数吗?或者其他更好的转换方式?

简而言之,我正在尝试从中提取 schemaIdlastModifiedDateavroBinaryValue.lengthavroBinaryValue C++ 中的字节数组..

我能够提取 schemaId 但我现在被困在其他事情上......

最佳答案

您的方法看起来不错,我只看到两个可能的问题。

  1. 如图所示,您的代码只是将 undefined variable data 转换为 uint64_t。确保您实际上是在通过数据缓冲区前进并转换正确的数据。

  2. 平台依赖。据我所知,并非所有平台都普遍支持 64 位字节交换函数(be64toh、betoh64、ntohll 等)。您可能需要在您的平台上使用不同的功能,和/或如果您希望代码独立于平台,则自动检测哪些功能有效。查看类似问题herehere ,例如。

至于如何获取数据,应该是这样的:

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];

关于c++ - 如何在 C++ 中从 bytearray 中提取字节时交换 64 位整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19393539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com