读取包含C++

发布于 2025-02-09 22:04:41 字数 1385 浏览 0 评论 0原文

我正在尝试使用stringstream来读取序列化数据的十六进制表示,我可以从数据库或文件中获得,例如“ 11007b000000 ...”,因此,如果我调用称为的函数, readShort前4个字符被解释为简短,那么如果我称为readInteger其余8个字符的函数,则为int等。到目前为止输出。 这是我的测试代码:

#include <iostream>
#include <sstream>

int main()
{
    std::string baseString = "11007B0000003A0400000A";
    std::stringstream baseStream(baseString);
    std::stringstream binaryStream(baseString, std::stringstream::out | std::stringstream::in | std::stringstream::binary);

    signed short result;
    // First attempt
    baseStream >> std::hex >> result;
    std::cout << result << std::endl;

    result = 0;
    binaryStream >> std::hex >> result;
    std::cout << result << std::endl;

    // Second Attempt
    baseStream.seekg(0, baseStream.beg);
    binaryStream.seekg(0, binaryStream.beg);
    result = 0;
    baseStream.read((char*)&result, sizeof(result));
    std::cout.write((const char*)&result, sizeof(result));

    result = 0;
    binaryStream.read((char*)&result, sizeof(result));
    std::cout.write((const char*)&result, sizeof(result));

    // Third attempt
    result = 0;
    baseStream >> result;
    std::cout << result;

    result = 0;
    binaryStream >> result;
    std::cout << result;
}

有更好的方法还是我缺少一些东西?

I am trying to use a stringstream to read through hex representation of serialized data which I can get from a DB or a file, eg "11007B000000..." so if I call a function called ReadShort the first 4 characters are interpreted as a short, then if I call a function called ReadInteger the remaining 8 characters as int, and so on. As of now I've tried istream, load a fstream from memory, opening stringstrem in binary mode, but I can't get the desired output.
This is my test code:

#include <iostream>
#include <sstream>

int main()
{
    std::string baseString = "11007B0000003A0400000A";
    std::stringstream baseStream(baseString);
    std::stringstream binaryStream(baseString, std::stringstream::out | std::stringstream::in | std::stringstream::binary);

    signed short result;
    // First attempt
    baseStream >> std::hex >> result;
    std::cout << result << std::endl;

    result = 0;
    binaryStream >> std::hex >> result;
    std::cout << result << std::endl;

    // Second Attempt
    baseStream.seekg(0, baseStream.beg);
    binaryStream.seekg(0, binaryStream.beg);
    result = 0;
    baseStream.read((char*)&result, sizeof(result));
    std::cout.write((const char*)&result, sizeof(result));

    result = 0;
    binaryStream.read((char*)&result, sizeof(result));
    std::cout.write((const char*)&result, sizeof(result));

    // Third attempt
    result = 0;
    baseStream >> result;
    std::cout << result;

    result = 0;
    binaryStream >> result;
    std::cout << result;
}

Is there a better approach or is there something I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文