我写了一个小测试文件来说明问题:
#include <iostream>
#include <iterator>
#include <algorithm>
#include <cstdio>
#include <sstream>
void printChar(const char c) {
std::string s(&c);
std::istringstream iss(s);
std::ostringstream oss;
std::copy(std::istream_iterator<char>(iss),
std::istream_iterator<char>(), // reads till the end
std::ostream_iterator<char>(oss));
std::string output = oss.str();
printf("%#x - %#x\n", c, output.c_str()[0]);
}
int main (const int argc, const char** argv) {
for (char i = 0; i < 0x20; ++i) {
printChar(i);
}
return 0;
}
现在,预期的输出将是
0 - 0
0x1 - 0x1
0x2 - 0x2
...
0x1e - 0x1e
0x1f - 0x1f
但是,我得到 0x9-0xD 的以下输出:
0x8 - 0x8
0x9 - 0x7f
0xa - 0x7f
0xb - 0x7f
0xc - 0x7f
0xd - 0x7f
0xe - 0xe
谁能解释我为什么得到这个结果?