• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading binary data  RSS feed

 
billo bailey
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have an assignment that requires reading binary data from a file. The file contains VM addresses and is of the following format, 32 bits, most significant bit is 1 or 0, least 20 significant bits contain an address. In approaching the assignment I thought I'd start simple and read the first byte only and skip the remaining three. I assumed this would give me only two possible results for each line of output, ie a 1 or 0. However it gave me multiple results. Could this be to do with the big endian, little endian? I'm not sure what platform the file was generated on.
Thanks for any help.
 
Rick Goldstein
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this depends on whether the data was written out as an int (32-bit), or as a byte stream. If an int, the byte order is architecture dependent, and you would need to read in the 4 bytes as an int. Then the lower level interaction with the machine takes care of little/big-endianness. If the bytes were written individually (or as a streamed byte array), then they'll be in whatever order they were written.

If the first byte seems to contain random numbers (and it's only supposed to contain 0x00 or 0x80, i.e. zero or 1 in the most significant bit), then the data was probably written as an int (and you're on a little endian machine). In that case, just read the int. If the high order bit is 1, the number will be negative, so that check is trivial. For the low 20 bits, you'll probably want to use a bit mask.

Rick
 
billo bailey
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The description only says the file is formatted as a sequence of 32-bit records. So I assume its an int as you say. I'll look into the bit mask, as its the first time I've heard of it.
Thanks for your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!