我想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;
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);
if (!flag) {
// I cannot just "cast" the byte array into a String
// value = reinterpret_cast<char*>(byteData);
// now how to retrieve schemaId, lastModifiedDate and actual_binary_value from byteData?
schemaId = *reinterpret_cast<uint16_t*>(byteData);
flag = false;
}
}
// this prints out 65407 somehow but it should be printing out 32767
cout<< schemaId <<endl;
}
如果有人需要查看我的 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++ 代码中做错了什么以及为什么我无法从它和其他字段中正确提取 schemaId?
更新:-
使用后 -
schemaId = ntohs(*reinterpret_cast<uint16_t*>(data));
我开始为 schemaId 正确取回值。
但是现在如何提取其他内容,例如lastModifiedDate
实际byteArray within
byteData and actual value of that
byteArray in
byteData 的长度。
我正在使用它,lastModifiedDate
但它不能以某种方式工作 -
std::copy(reinterpret_cast<uint8_t*>(byteData + 2), reinterpret_cast<uint8_t*>(byteData + 10), lastModifiedDate);