wood burning stoves
The moose likes Beginning Java and the fly likes No unsigned data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "No unsigned data" Watch "No unsigned data" New topic

No unsigned data

David Gerstman

Joined: Mar 21, 2001
Posts: 6
In the following code I read data from a binary file test.dat. I wish to convert the data; the problem is that there are no unsigned data types in Java. How do work around that limitation?
import java.io.*;
class ReadBin
public static void main (String args[])
byte buf [] = new byte [40];
byte byte1, byte2;
long tot;
FileInputStream f = new FileInputStream("test.dat");
f.read( buf );
} catch (Exception e)
System.out.println("Error: " + e.toString() );
System.out.println( buf );
for ( int i = 0; i < 20; i++ )
byte1 = (byte)buf[ 2 * i ];
byte2 = (byte)buf[ 2 * i + 1 ];
tot = byte2 + ( byte1 << 8 );
System.out.println( i + " " + byte1 + " " + byte2 + " " + tot );
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
maybe the Math class might have a helpful method?

giddee up
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
The readUnsignedShort() method of DataInputStream (and RandomAccessFile and the DataInput interface) does what you seem to be trying to do. Or you can do it yourself with bitwise operators to mask off the unwanted sign extensions like this:

By the way, your read() method isn't guaranteed to actually fill the buffer - many I/O operations can terminate before you expect because the requested bytes are not immediately available. They're giving you the option to do somethign else while waiting precious milliseconds for the hard drive to get into position. If you want to wait until all the data is really there, you need to do the read in a loop and check the return value. E.g.:
This is kind of tedious - it's probably easier to just read one byte at a time with the read() method (which will wait until at least one byte is ready before it returns). You can make this more efficient by wrapping f with a BufferedInputStream.

"I'm not back." - Bill Harding, Twister
David Gerstman

Joined: Mar 21, 2001
Posts: 6
Many thanks for your detailed response Jim. I thought there was something with the DataInputStream but I don't have really good references on how to use it.
(I tried the masking to no avail.)
And thank you for the warning about buffers not always filling immediately.
David Gerstman
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Note that Java does have an unsigned integral data type - char. I'm not suggesting that this would make this particular task easier
(to do so would require effort and thought).

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
David Gerstman

Joined: Mar 21, 2001
Posts: 6
But I was looking for a single byte data type. char is two bytes.
Consider Paul's rocket mass heater.
subject: No unsigned data
It's not a secret anymore!