🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: RandomAccessFile weirdness

#18529
From: Rich Wilkman
Date: Fri, 05 Jun 1998 00:00
86 lines
3168 bytes
Angus:

I just wrote a helper method to do the conversion for me.  If you read
your data into byte[] it's a little easier.  Here's my converter
methods to go int from 2 bytes, to int from one byte (not really
needed but it keeps the calling code cleaner) and 4 bytes to long:

  // 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[])


Hope this helps.

-Rich

angus m. fraser wrote:
> 
> In article <35781FFA.4E08492E@corp.borland.com>, rwilkman@corp.borland.com
> (Rich Wilkman) wrote:
> 
> > Angus:
> >
> > You're probably getting a "big indian, little indian" issue here.
> > When I was reading ints from a binary file I had to remember to
> > bitshift them because they're different in Java (to get around the
> > lack of a "signed" keyword).
> >
> > -Rich
> >
> 
> Thanks. That's what I thought was happening. It's going to be very tedious
> though having to read 2 bytes at a time and use a DIY method to convert
> them to the 'correct' short type.
> 
> Angus

-- 

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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: <357858F1.69D6F46@corp.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <35781FFA.4E08492E@corp.borland.com> <memo.19980605181318.252B@amf.btinternet.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,6e0494f6dbba0af4
X-Google-Attributes: gidf74b3,public
From: Rich Wilkman <rwilkman@corp.borland.com>
Subject: Re: RandomAccessFile weirdness
Date: 1998/06/05
Message-ID: <357858F1.69D6F46@corp.borland.com>
X-Deja-AN: 359943199
Content-Transfer-Encoding: 7bit
References: <35781FFA.4E08492E@corp.borland.com> <memo.19980605181318.252B@amf.btinternet.com>
Content-Type: text/plain; charset=us-ascii
Organization: Another Netscape Collabra Server User
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language