如何在C ++中从字节数组(在BIG-ENDIAN中)提取单个字段 [英] How to extract individual fields from byte array (which is in BIG-ENDIAN) in C++

查看:254
本文介绍了如何在C ++中从字节数组(在BIG-ENDIAN中)提取单个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从 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中提取上述特定信息 ...我能够以某种方式提取 schemaId ,但即将到来的值是错误的。而且我不确定如何提取其他内容从中...

Now I am trying to extract the above particular information from the byteData in C++... Somehow I am able to extract schemaId but the value which is coming is wrong.. And I am not sure how to extract other things from it...

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代码-

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)

有人可以帮我怎么回事我

Can anybody help me what wrong I am doing in my C++ code and why I am not able to extract schemaId properly from it and other fields as well?

更新:-

使用此命令后-

schemaId = ntohs(* reinterpret_cast< uint16_t *> (数据));

我开始正确获取schemaId的值。

I started getting the value back properly for schemaId.

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

But now how to extract other things such as lastModifiedDate, length of actual byteArray withinbyteDataand actual value of thatbyteArrayinbyteData`.

lastModifiedDate ,但它不起作用-

std::copy(reinterpret_cast<uint8_t*>(byteData + 2), reinterpret_cast<uint8_t*>(byteData + 10), lastModifiedDate);


推荐答案

32767是0x7fff。 65407是0xff7f。注意,高位和低位字节被交换。您需要交换这些字节以将数字恢复为原始值。幸运的是,有一个名为 ntohs (主机短网络)的宏或函数可以完全满足您的需求。这是宏还是函数,以及在哪个标头中定义取决于您的系统。但是,无论使用的是Windows,Linux,Sun还是Mac,宏/函数的名称始终为 ntohs

32767 is 0x7fff. 65407 is 0xff7f. Note that the high order and low order bytes are swapped. You need to swap those bytes to restore the number to the original value. Fortunately, there is a macro or function called ntohs (network to host short) that does exactly what you want. Whether this is a macro or function, and in which header it is defined, depends on your system. But the name of the macro/function is always ntohs, whether one is using Windows, Linux, Sun, or a Mac.

在小端机器上,此宏或函数交换形成16位整数的两个字节。在大型字节序计算机上,此宏/函数不执行任何操作(这正是所需的功能)。请注意,当今大多数家用计算机都是低位字节序。

On a little endian machine, this macro or function swaps the two bytes that form a 16 bit integer. On a big endian machine, this macro/function does nothing (which is exactly what is wanted). Note that most home computers nowadays are little endian.

这篇关于如何在C ++中从字节数组(在BIG-ENDIAN中)提取单个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆