Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从C+中的字节数组(以大端为单位)中提取单个字段+; 我想从我的C++代码中从下面的代码> ByTeDATAs/COD>中读出几个字节。byteData中的实际值是一个二进制blob字节数组,采用BIG-ENDIAN字节顺序格式。因此,我不能简单地将字节数组“转换”为字符串_C++_Bytearray_Bytebuffer_Endianness - Fatal编程技术网

如何从C+中的字节数组(以大端为单位)中提取单个字段+; 我想从我的C++代码中从下面的代码> ByTeDATAs/COD>中读出几个字节。byteData中的实际值是一个二进制blob字节数组,采用BIG-ENDIAN字节顺序格式。因此,我不能简单地将字节数组“转换”为字符串

如何从C+中的字节数组(以大端为单位)中提取单个字段+; 我想从我的C++代码中从下面的代码> ByTeDATAs/COD>中读出几个字节。byteData中的实际值是一个二进制blob字节数组,采用BIG-ENDIAN字节顺序格式。因此,我不能简单地将字节数组“转换”为字符串,c++,bytearray,bytebuffer,endianness,C++,Bytearray,Bytebuffer,Endianness,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 `byte

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++中的代码< ByTeDATAs/COD>中提取上述特定信息。不知何故,我能够提取出
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;
}
可以帮助我在C++代码中做什么错误,为什么我不能从它和其他字段中正确地提取Script? 更新:-

在使用本文件后—

schemaId=ntohs(*重新解释铸件(数据))

我开始正确获取schemaId的值

但是现在如何提取其他内容,例如
lastModifiedDate
,实际
byteArray在
byteData
中的长度,以及该
byteArray
byteData`中的实际值

我在
lastModifiedDate
中使用了这个,但不知怎的它不起作用--

std::copy(重新解释强制转换(byteData+2)、重新解释强制转换(byteData+10)、lastModifiedDate);

32767是0x7fff。65407是0xff7f。请注意,高阶字节和低阶字节是交换的。您需要交换这些字节以将数字恢复为原始值。幸运的是,有一个名为
ntohs
(网络到主机简称)的宏或函数正是您想要的。这是宏还是函数,以及在哪个标题中定义,取决于您的系统。但是宏/函数的名称总是
ntohs
,无论是使用Windows、Linux、Sun还是Mac


在小型endian机器上,此宏或函数交换形成16位整数的两个字节。在big-endian机器上,这个宏/函数什么都不做(这正是我们想要的)。请注意,现在大多数家用电脑都是little endian。

我应该在这里使用
ntohl
吗?
ntohs
可能是最好的选择。额外的好处:它将构造正确的值,而不管代码运行在哪个系统上。@IInspectable:谢谢,现在有意义了。。你知道我应该如何从中提取我问题中提到的其他字段吗?如果所有字段都以big-endian格式存储,你需要使用相应的
ntoh
变量将转换应用于所有字段。对于8字节字段,必须使用
ntohll
。我不知道这是否是标准的C++函数。@ LLNEXTABLE:谢谢你的建议。我已经试了很多次从bytearray中提取lastModifiedDate和其他内容,但每次都得到错误的结果。。你能帮我吗?为什么我需要交换字节?我在Java端以BIG-ENDIAN字节顺序格式存储它,对吗?那么它是否应该与ntohl合作?如果我的理解错误,请纠正我。你需要在你的C++代码中交换字节,因为你的机器是小字节,因为这个数字是用java代码存储在大字节序中的。它不会与<代码> ntoHL</代码>一起工作。最后的“l”表示“长”-32位。您正在存储一个短的--16位。您可以使用
ntohs
(s代表“short”)。谢谢,现在对我来说有点道理了。。除此之外,如何判断我的机器是BIG-ENDIAN还是LITTLE-ENDIAN?你不需要知道。在小型endian机器上,
ntohs
及其亲属(
htons
ntohl
htonl
)执行所需的字节交换,以在主机顺序和网络顺序之间转换。在big-endian机器上,这些函数只返回输入值作为输出。这些功能是可移植的;不管主机架构如何,它们都会做正确的事情。当然。。非常感谢你的帮助。。。现在回到我的第二个问题-如何提取
lastModifiedDate
以及其他内容?我试着这样做-
std::copy(reinterpret\u cast(byteData+2)、reinterpret\u cast(byteData+10)、lastModifiedDate)
但它对我不起作用?你能看出我做错了什么吗?
    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)
std::copy(reinterpret_cast<uint8_t*>(byteData + 2), reinterpret_cast<uint8_t*>(byteData + 10), lastModifiedDate);