🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #17043

Re: Newbie having trouble reading random access binary data file

#17043
From: Richard Wilkman
Date: Tue, 15 Dec 1998 00:00
81 lines
3500 bytes
Gary:

Yep.  You need converters to go from sets of bytes to primitive data
types.  It's done the usual way (bit shifting) in Java.  I've pasted
in some simple converter methods...

 // convert two bytes into an int, least significant bit first
  static final int toInt(byte[] bStreamIn){
     if(bStreamIn.length != 2)
        throw new IllegalArgumentException("int assumes 2 bytes");
     int part1 = (int)bStreamIn[0];
     int part2 = (int)bStreamIn[1];

     if( part1 < 0 )
        part1 += 256;

     return( part1 + (part2 << 8) );

  } // end of method toInt(byte[])

  // convert one byte to an int
  static final int charToInt(byte[] bIn){
     return (int)bIn[0];
  } // end of method  charToInt(byte[])

  // convert 4 bytes into a long, least significant bit first
  static final long toLong(byte[] bStreamIn){
     if(bStreamIn.length != 4)
        throw new IllegalArgumentException("long assumes 4 bytes");

        int part1 = (int)bStreamIn[0];
        int part2 = (int)bStreamIn[1];
        int part3 = (int)bStreamIn[2];
        int part4 = (int)bStreamIn[3];

        if(part1 < 0) part1 += 256;
        if(part2 < 0) part2 += 256;
        if(part3 < 0) part3 += 256;

        return( part1 + (part2 << 8) + (part3 << 16) + (part4 << 24)
);

  } // end of method toLong(byte[])

Gary Irwin wrote:
> 
> Hi:
> 
> I am new to Java but having a great time learning it - its a super language!
> I have a binary database file consisting of a mixture of strings, long ints
> and doubles. The database was originally created using Microsofts 16 bit 'C'
> 6.0 compiler (not VC6.0) running in DOS, therefore the strings are simple
> byte arrays, long ints are 4 byte and doubles 8 byte. I have successfully
> written read/write routines to the database using VB 6 and C++Builder 3, and
> am now moving to the Java platform with JBuilder 2 (standard).
> 
> I am using the RandomAccessFile class to open the file and am accessing the
> same using the seek() method. I can read the strings by reading with
> readByte(), casting to a char then concatenating to reconstitue the string -
> works fine. Unfortunately thats all I can read - when I use .readInt() to
> read a 4 byte int or an .readDouble() for an 8 byte double all I get is
> garbage. Is there something I am missing here?
> 
> Any advise would be much appreciated.
> 
> - Stranded programmer in the Great White North.

-- 

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Rich Wilkman
Senior Quality Assurance Engineer
Borland JBuilder Team

If you see something that looks like an opinion up there,
it's mine and mine alone.

Mail sent to me from the newsgroups won't bounce but will
be ignored.  If I have time, I'll respond, but you  have
a better chance if you confine your posts to the newsgroups
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Technical Details
Message-ID: <36768C2C.16682BEA@inprise.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <755po6$ojs2@forums.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,db8922f63d0c85da
X-Google-Attributes: gidf74b3,public
From: Richard Wilkman <rwilkman@inprise.com>
Subject: Re: Newbie having trouble reading random access binary data file
Date: 1998/12/15
Message-ID: <36768C2C.16682BEA@inprise.com>
X-Deja-AN: 422538396
Content-Transfer-Encoding: 7bit
References: <755po6$ojs2@forums.borland.com>
Content-Type: text/plain; charset=us-ascii
Organization: Another Netscape Collabra Server User
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language