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

时间:2013-10-16 00:51:40

标签: c++ bytearray endianness

我想从byteData中读取几个字节,如下面C ++代码中所述。 byteData内的实际值是BIG-ENDIAN字节顺序格式的二进制blob字节数组。所以我不能简单地将字节数组“转换”为String ..

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位整数?还是其他一些更好的转换方式?

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

我能够提取schemaId但我现在仍然坚持其他事情......

1 个答案:

答案 0 :(得分:1)

你的方法看起来很好,我看到的只有两个可能的问题。

  1. 您显示的代码只是将未定义的变量data转换为uint64_t。确保您实际上正在通过数据缓冲区并转换正确的数据。

  2. 平台依赖。从我所看到的,64位字节交换功能(be64toh,betoh64,ntohll等)并非在所有平台上普遍支持。您可能需要在平台上使用不同的功能,和/或如果您希望代码与平台无关,则可以自动检测哪些功能正常。例如,请参阅类似问题herehere

  3. 至于如何获取数据,这样的事情应该有效:

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