Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Reading binary data

 
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.
 
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.
 
The only thing that kept the leeches off of me was this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic