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

No unsigned data

 
David Gerstman
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
thanks,
David
import java.io.*;
class ReadBin
{
public static void main (String args[])
{
byte buf [] = new byte [40];
byte byte1, byte2;
long tot;
try
{
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
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe the Math class might have a helpful method?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Gerstman
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
David Gerstman
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I was looking for a single byte data type. char is two bytes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic