| Author |
How to read unsigned 4-byte numbers?
|
Robert Konigsberg
Ranch Hand
Joined: Jun 23, 2004
Posts: 172
|
|
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
|
SCJP 1.4 (91%)<br />SCJD 1.4 (376/400, 94%)
|
 |
Stefan Wagner
Ranch Hand
Joined: Jun 02, 2003
Posts: 1923
|
|
wouldn't that work: ?
|
http://home.arcor.de/hirnstrom/bewerbung
|
 |
Robert Konigsberg
Ranch Hand
Joined: Jun 23, 2004
Posts: 172
|
|
|
Probably not since readLong will read 8 bytes from the DataInput source.
|
 |
Julian Kennedy
Ranch Hand
Joined: Aug 02, 2004
Posts: 823
|
|
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
Joined: Jun 23, 2004
Posts: 172
|
|
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
Joined: Jan 30, 2000
Posts: 18670
|
|
[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'm not back." - Bill Harding, Twister
|
 |
Julian Kennedy
Ranch Hand
Joined: Aug 02, 2004
Posts: 823
|
|
Ha! Good point. Three cheers for testing - hip, hip... Jules
|
 |
Larry D. LeFever
Greenhorn
Joined: Aug 07, 2004
Posts: 6
|
|
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
Joined: Aug 03, 2004
Posts: 244
|
|
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 ]
|
 |
 |
|
|
subject: How to read unsigned 4-byte numbers?
|
|
|