【发布时间】:2013-10-15 22:12:36
【问题描述】:
我正在尝试从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;
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 withinbyteDataand actual value of thatbyteArrayinbyteData`的长度。
我将它用于lastModifiedDate,但它无法正常工作--
std::copy(reinterpret_cast<uint8_t*>(byteData + 2), reinterpret_cast<uint8_t*>(byteData + 10), lastModifiedDate);
【问题讨论】:
-
我应该在这里使用
ntohl吗? -
ntohs可能是要走的路。额外的好处:无论代码运行的系统的字节顺序如何,它都会构造正确的值。 -
@IInspectable:谢谢,现在说得通了。知道我应该如何从中提取其他字段,如我的问题中提到的那样吗?
-
如果所有字段都以大端格式存储,您需要使用相应的
ntoh<type>变体将转换应用于所有字段。对于 8 字节字段,您必须使用ntohll。我不知道这是否是一个标准的 C++ 函数。 -
@llnspectable:感谢您的建议。我已经尝试了很多方法来从 bytearray 中提取 lastModifiedDate 和其他内容,但每次我得到错误的结果。你能帮我解决这个问题吗?
标签: c++ bytearray bytebuffer endianness