简体   繁体   English

如何在 C++ 中创建带有无符号字符数组的 streambuf

[英]How to create streambuf with array of unsigned char in C++

I want to create a std::istream object with a stream buffer object that can take raw byte data from array of unsigned char.我想创建一个带有 stream 缓冲区 object 的std::istream object ,它可以从无符号字符数组中获取原始字节数据。 I searched and found this Link我搜索并找到了这个链接

However they create the stream buffer based on array char:但是,他们基于数组 char 创建了 stream 缓冲区:

struct membuf : std::streambuf
{
    membuf(char* begin, char* end) {
        this->setg(begin, begin, end);
    }
};

I thought about type caste, but i don't want to modify the original data.So how i can it be done using unsigned char.我考虑过类型种姓,但我不想修改原始数据。那么我如何使用 unsigned char 来完成它。

With std::istream you cannot use unsigned char explicitly, because it is a typedef for std::basic_istream<char> docs .使用std::istream您不能显式使用unsigned char ,因为它是std::basic_istream<char> docs的 typedef。 You can cast your buffer pointers to char*您可以将缓冲区指针转换为char*

this->setg(reinterpret_cast<char*>(begin), reinterpret_cast<char*>(begin), reinterpret_cast<char*>(end));

Note that conversion of values greater than CHAR_MAX to char is implementaion defined (of course, only if you will actually use this values as char ).请注意,将大于CHAR_MAX的值转换为char实现定义的(当然,只有当您实际将此值用作char时)。

Or you can try to use std::basic_istream<unsigned char> (I have not tried it though).或者您可以尝试使用std::basic_istream<unsigned char> (虽然我没有尝试过)。

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

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