PPM 将字符转换为 int 授予负数
PPM conversion char to int grants negative numbers
我的任务是将字符从 PPM 图片转换为 int 数组,然后在该数组上进行其他转换。我得到了公式,它应该将 PPM 文件中的 3 个字符(文件中的红色、绿色和蓝色顺序(转换为一个 int 变量
int main(){
ifstream obraz1;
obraz1.open("start_1.ppm", ios::in);
int x,y,colors;
char proba[10000];
string firstline; //P6
getline(obraz1,firstline); //p6 scan
//
string line;
getline(obraz1,line);
sscanf(line.c_str(),"%d %dn",&x,&y);
getline(obraz1,line);
sscanf(line.c_str(),"%dn",&colors);
int lenghtwithoutheader=obraz1.tellg();
cout<<firstline<<" "<<endl;
cout <<x<<" "<<y<<" "<<" "<<colors<<endl;
/////////////////LOAD////////////////////////
int length;
obraz1.seekg(0, std::ios::end);
length = obraz1.tellg(); // report location (this is the length)
obraz1.seekg(lenghtwithoutheader, ios::beg);
char* buffor;
cout<<"full pic: "<<length<<"pic without head : "<<lenghtwithoutheader<<endl;
buffor = new char[length-lenghtwithoutheader];
obraz1.read(buffor,length-lenghtwithoutheader);
int *tabobraz=new int[(length-lenghtwithoutheader)/3];
for(int i=0;i<length-lenghtwithoutheader;i=i+3){
tabobraz[i]=(buffor[i]*(256*256))+(buffor[i+1]*256)+buffor[i+2];
cout<<"to int "<<(int)buffor[i]<<" "<<(int)buffor[i+1]<<" "<<(int)buffor[i+2]<<" "<<endl;
cout<<i<<" "<<buffor[i]<<" "<<buffor[i+1]<<" "<<buffor[i+2]<<" "<<endl;
}
return 0;
}
不知何故,我最终得到了负数(这对于图片来说是不可能的(:
to int 112 -72 -18
2247 p Ş ţ
我的第一个想法是,也许我不知何故溢出了 int 并且它又回到了负数,但这是不可能的,我可以使用该数字的最大字节数是 16-20 个字节中的 32 个字节。我在不同的系统和硬件上测试了它,所以我认为这不是那么重要。我怎样才能让它工作?
问题是char
在您的实现上签名(char
的签名是特定于实现的(。
读取以下内容时使用无符号缓冲区和强制转换:
unsigned char* buffor = new unsigned char[length - lenghtwithoutheader];
obraz1.read(reinterpret_cast<char*>(buffor), length - lenghtwithoutheader);
相关文章:
- 按字符值访问int数组
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 将转换字符键入 int 以用作向量C++的索引
- 在 std::无符号字符的向量处存储 int 的十六进制表示形式
- 从 std::vector<无符号字符>切片中提取 int?
- QDial:如何将 int 值传递给需要常量字符*的 Qfile?
- C++:使用没有位移位的指针将无符号字符转换为无符号 int
- 将子字符串字符从字符串值转换为 int,然后将其分配给 int 变量
- int 数组,但索引是字符?
- 如何将矢量<int>转换为字符数组?
- 仅读取文件末尾的最后 3 个字符,并将其另存为 int
- 递增 'int i = 0' 以在初始化的字符数组的位置递增
- 在 c++ 中逐个字符从字符串传递到 int 时不精确
- 将 static_cast<int>(-15) 分配给静态常量字符类型变量
- PPM 将字符转换为 int 授予负数
- 第 5 行:字符 54:错误:调用"min(int,std::__cxx11::basic_string<char>::size_type)"没有匹配函数
- 将char类型指定给int以打印相应的ASCII字符
- 为什么采用 int 的函数重载优先于采用无符号字符的函数重载?
- 我应该使用无符号字符而不是 int 来存储永远不会大于 255 的值吗?
- reinterpret_cast<int *>(字符 *)