我想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`.
现在我正在尝试从byteData
C++ 中提取上述特定信息......不知何故我能够提取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++ 中的那个字节数组中提取schemaId
、lastModifiedDate
和..avroBinaryValue.length
avroBinaryValue
我能够提取schemaId
,但我现在被困在其他事情上......