简体   繁体   English

C++ 扩展 Ascii 和负值

[英]C++ Extended Ascii, and negative values

I am trying to read some values from a binary file.我正在尝试从二进制文件中读取一些值。 I read byte by byte.我逐字节读取。 I just want to store each int value from a certain char into a buffer, sized as the number of bytes contained in the file..So I expect to have values between 0 and 255 stored int the buffer.我只想将来自某个字符的每个 int 值存储到一个缓冲区中,大小为文件中包含的字节数..所以我希望将 0 到 255 之间的值存储在缓冲区中。 But I am getting some negative ones some times..Does anyone know how can this problem be fixed?但有时我会收到一些负面影响..有谁知道如何解决这个问题? I would appreciate your response.我会很感激你的回应。

I expect to have values between 0 and 255 stored int the buffer.我希望将 0 到 255 之间的值存储在缓冲区中。

This suggests you want the value range of unsigned char .这表明您想要unsigned char的值范围。

But I am getting some negative ones some times但有时我会收到一些负面消息

This suggests you are reading signed values.这表明您正在读取有符号值。 The computer will not do something you do not tell it to do.计算机不会做你没有告诉它做的事情。 If you don't want signed values, don't allow that possibility.如果您不想要带符号的值,请不要允许这种可能性。 The signed char type allows negative values, the char type may (depending on the system) allow negative values, and the unsigned char type does not allow negative values.signed char类型允许负值, char类型可能(取决于系统)允许负值, unsigned char类型不允许负值。 Simply choose the correct tool for the job at hand.只需为手头的工作选择正确的工具。

It's just a guess without seeing your code, but I'm pretty sure you are using an array of char for your buffer when you should be using either an array of unsigned char or an array of std::byte (a type that implements the concept of a byte without the baggage of interpreting bytes as characters, useful when reading a binary file).这只是一种猜测,没有看到你的代码,但我敢肯定,你正在使用的数组char为您的缓冲区时,你应该用两种阵列unsigned char或数组std::byte (一个类型实现了字节的概念,没有将字节解释为字符的包袱,在读取二进制文件时很有用)。

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

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