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

标签 c++ arrays endianness

我正在尝试从 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/

相关文章:

c++ - 为什么在动态分配的数组中初始长度不能为 1?

javascript - 在 Ember 中使用数据库中的 JSON 显示数组

c# - C++ 中的结构大小 & 转换为 C#

c++ - 用户创建的 header 导致 C2061 : Syntax Error : Identifier 'classname'

c++ - 播放 "clicking"噪音

c++ - C++ std::hex 是否反转字节顺序? (将十进制打印为十六进制)

linux - glibc 文档和字节顺序

c++ - 如何在 Visual Studio 6.0 中调试在 ASP 页面中创建的 COM 对象?

c - 如何用 cycle-for 和 scanf 填充我的结构数组的每个结构?

ruby - 转换三个字节,二进制补码,有符号整数