我有一个以二进制数据(无编码)保存的原始图像文件。我想读入文件并将值转换为无符号字符。但我不确定如何开始这样做。每个文件包含 640x480 字节。每个像素为 8 位。
我在这里使用了 C++ 帮助页面:http ://www.cplusplus.com/doc/tutorial/files/,但是当我计算数据时,它似乎显示了相同的二进制/非人类可读字符。有人可以建议吗?到目前为止,这是我的代码:
#include <iostream>
#include <fstream>
using namespace std;
ifstream::pos_type size;
char * memblock;
int main () {
ifstream file ("imageData.raw", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
cout << "the complete file content is in memory";
for (int l=0; l<size; l++){
cout << " memblock[] is = " << (unsigned char)memblock[l] << " index was l = " << l << endl;
}
file.close();
delete[] memblock;
}
else cout << "Unable to open file";
return 0;
}
这是输出的示例捕获:
memblock[] is = ▀ index was l = 2937
memblock[] is = E index was l = 2938
memblock[] is = τ index was l = 2939
memblock[] is = C index was l = 2940
memblock[] is = ┘ index was l = 2941
memblock[] is = B index was l = 2942
memblock[] is = ╬ index was l = 2943
memblock[] is = D index was l = 2944
memblock[] is = ┼ index was l = 2945
memblock[] is = C index was l = 2946
memblock[] is = ╝ index was l = 2947
memblock[] is = B index was l = 2948
memblock[] is = ┤ index was l = 2949
memblock[] is = B index was l = 2950
memblock[] is = ¿ index was l = 2951
memblock[] is = > index was l = 2952
memblock[] is = í index was l = 2953
memblock[] is = ; index was l = 2954
memblock[] is = £ index was l = 2955
memblock[] is = 6 index was l = 2956
memblock[] is = á index was l = 2957
memblock[] is = 4 index was l = 2958
memblock[] is = Ñ index was l = 2959
memblock[] is = 7 index was l = 2960
memblock[] is = ╡ index was l = 2961