如何交换64位整数,而在C ++中从bytearray提取字节? [英] How to swap 64 bit integer while extracting bytes from bytearray in C++?
问题描述
我正在读取几个字节从 byteData
如下所述在我的C ++代码。 byteData
中的实际值是BIG-ENDIAN字节顺序格式的二进制blob字节数组。因此,我不能简单地将字节数组转换为字符串。
I am tring to read couple of bytes from byteData
as mentioned below in my C++ code. The actual value within byteData
is a binary blob byte array in BIG-ENDIAN byte order format. So I cannot simply just "cast" the byte array into a String..
byteData
字节数组由这三个东西 -
byteData
byte array is composed of these three things -
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
,我得到的值也是正确的..现在我不知道如何提取其他的东西...
Now I am trying to extract the above particular information from the byteData
in C++... Somehow I am able to extract schemaId
and the value which I am getting is also correct.. Now I am not sure how to extract other things from it...
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..
}
If somebody needs to see my java code then this is my java code -
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位整数这里?
Can anybody help me what wrong I am doing in my C++ code and why I am not able to extract lastModifiedDate
properly from it and other fields as well? As I understand lastModifiedDate
is 64 bit integer, so is there any way to swap out 64 bit integer here? Or some other better way to do the conversion?
简而言之,我试图提取 schemaId
,<$ c ++中的字节数组中的c $ c> lastModifiedDate , avroBinaryValue.length
和 avroBinaryValue
..
In short, I am trying to extract schemaId
, lastModifiedDate
, avroBinaryValue.length
and avroBinaryValue
from that byte array in C++..
我可以提取 schemaId
但我现在卡住了其他东西...
I am able to extract schemaId
but I am stuck on other things now...
推荐答案
您的方法看起来不错,我看到的只有两个可能的问题。
Your method looks fine, there are just two possible issues I see.
-
您所显示的代码只是将一个未定义的变量
转换为
uint64_t
。请确保您实际上正在通过数据缓冲区并转换正确的数据。
Your code as shown is simply casting an undefined variable
data
as auint64_t
. Make sure that you are actually advancing through the data buffer and converting the correct data.
平台相关性。从我看到的,64位字节交换功能(be64toh,betoh64,ntohll等),并不是所有平台上普遍支持。您可能需要在平台上使用不同的功能,并且/或者如果您希望代码与平台无关,则自动检测哪些功能有效。请参阅类似问题此处和这里。
Platform dependency. From what I've seen, the 64-bit byte swapping functions (be64toh, betoh64, ntohll, etc), are not universally supported on all platforms. You may need to use a different function on your platform, and/or automatically detect what works if you want the code to be platform independent. See similar questions here and here, for example.
至于如何获取数据,这样的工作应该是:
As to how to get the data, something like this should work:
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];
这篇关于如何交换64位整数,而在C ++中从bytearray提取字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!