如何在C++中表示二进制字节数组数据? 我试图用C++中的LICBQL库从Casdand检索数据。我在Cassandra中创建的表格是这样的- create table test_cql (user_id text, column_name text, column_value blob, primary key (id, column_name));

如何在C++中表示二进制字节数组数据? 我试图用C++中的LICBQL库从Casdand检索数据。我在Cassandra中创建的表格是这样的- create table test_cql (user_id text, column_name text, column_value blob, primary key (id, column_name));,c++,bytearray,endianness,C++,Bytearray,Endianness,下表为上表中的数据: cqlsh:testks> select column_name, column_value from test_cql where user_id = '1'; column_name | column_value --------------------------+-------------------------------------------------------------------------------------

下表为上表中的数据:

cqlsh:testks> select column_name, column_value from test_cql where user_id = '1';

 column_name              | column_value
--------------------------+--------------------------------------------------------------------------------------------
 @hello.1381780131229.dc1 | 0x7fff0000012c4ebb95550000001e42797465204172726179205465737420466f722042696720456e6469616e
这里的列_值是实际的blob值,这是我试图检索的

现在是我将尝试从CasDANRA检索数据的C++代码,用于UsRyID=1,其中我尝试打印列名称和列值细节。< /P>

    bool flag = false;
    std::map<std::string, std::string> m;
    std::string key, value;

    string query = "select column_name, column_value from test_cql where user_id ='1';";
    std::cout << query << endl;

// the below line will execute the query
    cql_result_t& result = execute_query(query);

// this will print out the result after executing the above query

    while (result.next()) {
        for (size_t i = 0; i < result.column_count(); ++i) {
            cql::cql_byte_t* data = NULL;
            cql::cql_int_t size = 0;
            result.get_data(i, &data, size);

            if (!flag) {
                key = reinterpret_cast<char*>(data);
                flag = true;
            } else if (flag) {
                value = reinterpret_cast<char*>(data);
                m[key] = value;
                flag = false;
            }
        }

        cout<<key << "-" << value <<endl;

    }
上面的代码只打印出key为@hello.1381780131229.dc1,而不是值。。它应该以0x7FFF0000012C4EBB95550000001E4279746520417272617920546737420466F7220426967220456E6469616E的形式打印出值,因为该值是Cassandra表中的字节数组二进制blob,即列_值

我已经将键和值都声明为字符串,这可能是我在上面代码中猜测的问题。。但我相信这个值是Cassandra表列_value中的实际blob。。因此,我不知道如何使用上面的C++代码检索二进制BLB字节数组?p> 而且这个二进制字节数组blob值可以是任意长度的,它总是以BIG-ENDIAN字节顺序格式存储


来自Java背景,我有点小问题。。。在此方面如有任何帮助,将不胜感激。。。我猜,它的完全C++问题就像我应该用哪一个数据类型来表示二进制字节数组…

,因为二进制块不是字符串,所以你不能把它打印成字符串。您必须手动打印字符串中的每个字节

如果您想知道为什么,让我们看看前三个字节:0x7f、0xff和0x00。第一个字节和第二个字节都不可打印,第三个字节是字符串终止符,因此如果将其打印为字符串,打印将停止

如果你有C++11,你可以这样做

std::cout << "Value = 0x";
for (const char& byte : value)
    std::cout << std::hex << static_cast<int>(byte);
std::cout << std::endl;
如果数据实际上是一个结构,则可以为其使用结构:

struct employee
{
    uint16_t id;
    uint8_t lastModifiedDate[8];
    std::string value;
};

...

std::map<std::string, employee> m;

...

m[key].id = *reinterpret_cast<uint16_t*>(data);
std::copy(reinterpret_cast<uint8_t*>(data + 2),
          reinterpret_cast<uint8_t*>(data + 10),
          m[key].lastModifiedData);
m[key].value = std::string(reinterpret_cast<char*>(data + 10), size - 10);

您不能将字节数组强制转换为字符串,因为许多字节值将不可平移。如何将字节转换为字符串表示的代码示例非常常见。到底是什么问题?谢谢你的建议。。。。你指出了我们更精细的细节。。让我给你详细解释一下。。这个字节数组由-前两个字节将包含我的employeeId,接下来的8个字节将是lastModifiedDate,剩余的字节是我的实际值。。。既然你说了,我需要手动打印每个字节。。是否可以从该字节数组中提取前两个字节、后八个字节以及剩余的字节值?注意:此字节数组采用big-endian字节顺序格式。。还有,这里的ch是什么?@TrekkieTechieT-T对于解析数据,您可以使用正确类型的结构,并将其转换为正确类型。将更新我的答案。非常感谢您的帮助。。这里我的employeeId是java中的短数据类型,有两个字节。。lastModifiedDate是java中的长数据类型,它有八个字节,其余的也是可变长度的bytearray。。。所以我需要从字节数组中提取这三样东西;这里的X我们不知道它会是什么,因为它可以是可变长度的。。
struct employee
{
    uint16_t id;
    uint8_t lastModifiedDate[8];
    std::string value;
};

...

std::map<std::string, employee> m;

...

m[key].id = *reinterpret_cast<uint16_t*>(data);
std::copy(reinterpret_cast<uint8_t*>(data + 2),
          reinterpret_cast<uint8_t*>(data + 10),
          m[key].lastModifiedData);
m[key].value = std::string(reinterpret_cast<char*>(data + 10), size - 10);