【问题标题】:converting unsigned char* to std::istream* C++将 unsigned char* 转换为 std::istream* C++
【发布时间】:2018-11-02 23:04:51
【问题描述】:

我必须将二进制数据(无符号字符 *)传递给以 std::istream 作为参数的函数 PerformRequest。什么是最好的

unsigned char* data // has the binary data

PerformRequest(std::istream* in)
{
    //some implementation
}

【问题讨论】:

  • 看看stringstream

标签: c++


【解决方案1】:

您可以使用<sstream> 中的std::stringstream,它支持istreamostream 接口。所以你可以通过ostream-interface 写入数据,然后作为istream-argument 传递:

#include <sstream>
#include <iomanip>
#include <iostream>

void prints(istream &is) {
    unsigned char c;
    while (is >> c) {
        std::cout << "0x" << std::hex << (unsigned int)c << std::endl;
    }
}

int main()
{
    unsigned char x[6] = { 0x2, 0x10, 0xff, 0x0, 0x5, 0x8 };
    std::stringstream xReadWrite;
    xReadWrite.write((const char*)x, 6);
    prints(xReadWrite);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 2021-07-11
    • 2014-02-19
    • 2020-05-03
    • 1970-01-01
    相关资源
    最近更新 更多