-2

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. But I am getting some negative ones some times..Does anyone know how can this problem be fixed? I would appreciate your response.

DiMis
  • 69
  • 1
  • 3
    please show your code ([mcve]) – 463035818_is_not_an_ai Dec 21 '19 at 13:47
  • 1
    I suppose you're using `char` type to store your data. `char` is signed, which means it can contain negative values. If you want to interpret the values from 0 to 255, use `unsigned char` or `uint8_t`. But these are just assumptions and you should really post the relevant code if you want us to help you. – ProXicT Dec 21 '19 at 13:47
  • 1
    show some example of what you have done yet. –  Dec 21 '19 at 13:49
  • 3
    @ProXicT "`char` is signed" - *Wrong*. Whether `char` is signed or unsigned is *implementation defined*. There are 3 *distinct* character types in C and C++: `signed char`, `unsigned char` and `char`. `char` is its own distinct type from the other two and whether or not it is signed is implementation defined. Don't assume `char` is signed, you'll be in for some nasty surprises when porting your code to other platforms. For example; on AiX with the Xlc compiler, `char` is unsigned. – Jesper Juhl Dec 21 '19 at 13:55
  • 1
    @JesperJuhl Alright, didn't know that, thanks for telling me! :) All compilers I've worked with, which frankly isn't that many, have `char` as signed. But it's good to know it's not defined by the standard, but implementation defined! – ProXicT Dec 21 '19 at 13:58
  • @JesperJuhl That's as may be, but the behavior OP is experiencing strongly suggests that `char` is signed in this case. – Spencer Dec 21 '19 at 14:04
  • @Spencer I did not comment on OPs code. I commented on the statement by ProXicT that said that `char` is *always* signed. Which is false. – Jesper Juhl Dec 21 '19 at 14:08
  • @JesperJuhl I don't see the word "always" in the comment you responded to. – Spencer Dec 21 '19 at 14:10
  • @Spencer "`char` is signed" reads like a statement of fact. But whatever, this is not a discussion forum. I just saw a false statement, commented to correct it, that's all. I'm not going to get dragged into some silly argument, so this is the last I have to say on that topic. – Jesper Juhl Dec 21 '19 at 14:13
  • Guys I just want to thank everyone for your help. My problem was on writing the file. I used unsigned char and I did my job. Please don't judge me, I just started learning c++ and still I haven't got used to some basic stuff. Again, I just want to thank each of you, I really appreciate it! – DiMis Dec 21 '19 at 15:17
  • My goal was to read an PPM image and store it into a buffer. I just want to make few changes to the pixels, and then after write it again to a new image. – DiMis Dec 21 '19 at 15:19
  • @ProXicT [Why don't the C or C++ standards explicitly define char as signed or unsigned?](https://stackoverflow.com/q/15533115/995714), [Is char signed or unsigned by default?](https://stackoverflow.com/q/2054939/995714). On ARM it's often unsigned because [that's more efficient](https://stackoverflow.com/a/6532932/995714) – phuclv Dec 21 '19 at 15:45
  • @phuclv That was actually the first resource I read when I found out that `char` signedness is implementation defined. Thanks though! :) – ProXicT Dec 21 '19 at 21:45

1 Answers1

0

I expect to have values between 0 and 255 stored int the buffer.

This suggests you want the value range of 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. 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).

JaMiT
  • 14,422
  • 4
  • 15
  • 31