• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read unsigned 4-byte numbers?  RSS feed

 
Robert Konigsberg
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

In java.io.DataInput, there are methods readUnsignedByte (1-byte read), readUnsignedShort (2-byte read) but there is no readUnsignedInt(4-byte read). If I use readInt() it's a signed value. Any thoughts as to the easiest way I can read an unsigned 4-byte value? OF COURSE I'll store it as a long, but I'm lost. I could do it myself, but that's definitely not what I want to do, as you can imagine.

Tell me they've fixed this in 1.5. Or 5.0. Or whatever.

Thanks!

Rob
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wouldn't that work:
?
 
Robert Konigsberg
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably not since readLong will read 8 bytes from the DataInput source.
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about amending Stefan's suggestion as follows:

I'm sure you can do something clever with the bit shifting operators instead of the second line but I'll leave that to you, if you like.

Jules
 
Robert Konigsberg
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh, yeah! D'uh.

I have an ulterior reason for understanding this particular problem. I'll discuss in another thread.

Thanks, RK
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[B][Julian]: What about amending Stefan's suggestion as follows:
[/B]
Still two problems. One is that 'l' is a truly horrible name for a variable, since it looks so much like 1. The other is that the code doesn't work. Try:

or

or

[ August 10, 2004: Message edited by: Jim Yingst ]
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ha! Good point. Three cheers for testing - hip, hip...

Jules
 
Larry D. LeFever
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
[B][Julian]: What about amending Stefan's suggestion as follows:
[/B]
Still two problems. One is that 'l' is a truly horrible name for a variable, since it looks so much like 1. The other is that the code doesn't work. Try:

or

or


[ August 10, 2004: Message edited by: Jim Yingst ]


I believe I understand that last technique (that adding "1" to the 9th bit will force that set bit and all the higher-order ones over to zero, right?).

However, I'd appreciate some explanation of your first two techniques. I presume you're exploiting certain details of two's complement notation, but I haven't figured out just what you're doing there.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

will not work, 2nd statement does nothing for Integer.MIN_VALUE<=val<0.

Correct:

or


[ August 14, 2004: Message edited by: Vlado Zajac ]
[ August 14, 2004: Message edited by: Vlado Zajac ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!