繁体   English   中英

将未签名的char *转换为std :: istream * C ++

[英]converting unsigned char* to std::istream* C++

我必须将二进制数据(无符号char *)传递给以std :: istream为参数的PerformRequest函数。 哪个最好

unsigned char* data // has the binary data

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

您可以使用<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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM