• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

No unsigned data

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 );
}
}
}
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe the Math class might have a helpful method?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I was looking for a single byte data type. char is two bytes.
 
I'm still in control here. LOOK at this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic