🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Bad integer values when converting from DOS, Basic

#17605
From: Richard Wilkman
Date: Mon, 26 Oct 1998 00:00
78 lines
3269 bytes
John:

That's just the way Java works.  Doesn't have "unsigned" types,
either.

Here's a routine that I use for the same basic work.  It takes a
byte[], but since you are reading from a stream you shouldn't have to
change it:

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


I have a similar routine for moving from 4 bytes to a long, in case
you need to work with floating point values at some point.  It looks
like:

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

-Rich

John F. Dutcher wrote:
> 
> Java users: Can any comment on the following integer problem ?
> I am converting a system from DOS written in Microsoft Quick
> Basic (compiled). I have had to discover that the 2 byte integer
> in DOS based files use a reverse byte format from those used
> in JAVA. This is the big endian, little endian dilema for those
> acquainted with those terms. When reading the DOS files for
> conversion with JAVA the integer values are, of course, all wrong.
> I tried simply 'byte switching' which seemed like the appropriate
> and simple solution to getting JAVA integers in the 'endian' format
> it wants. This does not produce good values however. (I don't
> know why). Has anyone seen or used a solution that will covert
> these DOS, Basic integers to true valued JAVA integers ??

-- 

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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: <3634C27E.5D0DA706@inprise.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <01bdffb3$c59ef600$2b6fcad0@jfdutcher>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,8670dec9948c4a7e
X-Google-Attributes: gidf74b3,public
From: Richard Wilkman <rwilkman@inprise.com>
Subject: Re: Bad integer values when converting from DOS, Basic
Date: 1998/10/26
Message-ID: <3634C27E.5D0DA706@inprise.com>
X-Deja-AN: 405272579
Content-Transfer-Encoding: 7bit
References: <01bdffb3$c59ef600$2b6fcad0@jfdutcher>
Content-Type: text/plain; charset=us-ascii
Organization: Another Netscape Collabra Server User
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language