wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Reading Bytes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading Bytes" Watch "Reading Bytes" New topic
Author

Reading Bytes

Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 72

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.


Da mihi sis bubulae frustum assae, solana tuberosa in modo Gallico fricta ac quassum lactatum coagulatum crassum.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 72

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
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

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

Joined: Sep 28, 2012
Posts: 72

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

For some background info, see Endianness on Wikipedia.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
I think this Wikipedia page might be more useful

And yes, you do have to eat the eggs.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

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

Joined: Mar 17, 2011
Posts: 7492
    
  18

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading Bytes
 
Similar Threads
MD5
Out of Memory exception
writing 4 byte little-endian long integers
Convert 2 bytes to a a Int
Java endian and bit order and signed and unsigned and python and more confusion than I can deal with