Search code examples
javabinaryfiles

I need help pertaining to binary files


When I use writeByte(346); to a binary file and then do readByte(); it returns 90, not 346. I'm perplexed and would greatly appreciate some help.


Solution

  • 346 doesn't fit in a single byte.

    So 346 overflows and you get 346 - 256 (2^8) = 90.

    You need at least 2 bytes to store 346.


    writeByte(int) takes a signed int as parameter, so the limits for one byte are -128 to 127