🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Casting byte[] to long or String

#18434
From: Richard Wilkman
Date: Wed, 17 Jun 1998 00:00
58 lines
2125 bytes
Stephen:

Going to String is easy -- there's a Constructor you can use for that
hanging off of String.

To go to long you need to do some bit twiddling to keep the Big Indian
and the Little Indian in line.  Here's the method I use...

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

That should do the trick.

-Rich

Stephen J. Cherin wrote:
> 
> I need to convert/cast a byte[] to either a long or String.  How is this
> done?
> 
> --
> 
> _________________________________
> Stephen J. Cherin
> stephen@cherin.com
> www.cherin.com

-- 

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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: <358825BC.89FD78EA@inprise.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <358802E2.FDD8D9A0@cherin.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,5ac476416589261b
X-Google-Attributes: gidf74b3,public
From: Richard Wilkman <rwilkman@inprise.com>
Subject: Re: Casting byte[] to long or String
Date: 1998/06/17
Message-ID: <358825BC.89FD78EA@inprise.com>
X-Deja-AN: 363626759
Content-Transfer-Encoding: 7bit
References: <358802E2.FDD8D9A0@cherin.com>
Content-Type: text/plain; charset=us-ascii
Organization: Another Netscape Collabra Server User
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language