Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading Bytes

 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to read in 4 bytes, then using a shift operator, merge it into a single int.

I'm wrote this code:


I'm not sure how I'm supposed to use shift operators, so I used the count variable. Can I do it that way or should I learn to use shift operators instead?

Thanks in advance.
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aj Prieto wrote:I'm trying to read in 4 bytes, then using a shift operator, merge it into a single int.


First, what do you mean by "merge it into a single int"?? Are these four bytes, the four bytes that make up the bytes of the integer? Meaning if the four bytes are 0x02, 0x34, 0xAB, and 0x21, then the int is 0x21AB3402 (assuming little endian). If so, then yes, shifting is a good way to do it.

Henry
 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the 4 bytes make up the integer. What do you mean by little endian?

I've tried using bit operators, though I'm not entirely sure if I'm using it correctly.


Am I using it the right way? Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aj Prieto wrote:Yes, the 4 bytes make up the integer. What do you mean by little endian?


Basically, "little endian" are people (from Liliput) who believe that hard-boiled eggs should be eaten from the little end first... In computer terms, little endian means the least significant byte comes first. So, the number 0x12345678, is stored in memory as 0x78 0x56 0x34 0x12 -- meaning the 0x78 byte is stored in the lowest memory address.

Aj Prieto wrote:
I've tried using bit operators, though I'm not entirely sure if I'm using it correctly.


Am I using it the right way? Thanks


Yes. It is correct. But as an FYI, you coded it for big endian -- so you like to eat the big end of the hard boiled egg first...

Henry
 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Basically, "little endian" are people (from Liliput) who believe that hard-boiled eggs should be eaten from the little end first... In computer terms, little endian means the least significant byte comes first. So, the number 0x12345678, is stored in memory as 0x78 0x56 0x34 0x12 -- meaning the 0x78 byte is stored in the lowest memory address.


That's interesting, sounds like a fun place to live. lol

Henry Wone wrote:
Yes. It is correct. But as an FYI, you coded it for big endian -- so you like to eat the big end of the hard boiled egg first...

Henry


Awww...but I dislike eggs . Is there an option to not eat the eggs? .

It is as simple as just switching the operator from << to >>?

edit: When I run a .huff file through it, I get a negative "count" value. Why is this happening, can I add 256 to it to get the correct value?

Thanks

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15484
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some background info, see Endianness on Wikipedia.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this Wikipedia page might be more useful

And yes, you do have to eat the eggs.
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aj Prieto wrote:
It is as simple as just switching the operator from << to >>?


No.... but it is as simple as doing the loop backwards (of course, that means that you need to use a regular loop -- not a foreach loop).

Aj Prieto wrote:
edit: When I run a .huff file through it, I get a negative "count" value. Why is this happening, can I add 256 to it to get the correct value?


Remember that integers are signed in Java, so for big endian, if the first byte is a negative number, then the result will be a negative number. As for correct value, that negative number is the correct value -- if you need a positive value, then you need to put it into something that can hold the four bytes as a positive number, such as a long type.

Henry
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Remember that integers are signed in Java, so for big endian, if the first byte is a negative number, then the result will be a negative number...

Providing you ALWAYS read in 4 bytes (or multiples of). If not, you'll probably need an extra shift at the end to maintain the correct endianness.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic