🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Byte Array Conversion to Integer

#7990
From: John Pruitt
Date: Mon, 07 May 2001 19:38
53 lines
3023 bytes

There are a couple of ways of doing what you need.

This one reads the data directly from a network connection:

  Socket s = net Socket(host,port);
  InputStream ins = s.getInputStream();
  DataInputStream dis = new DataInputStream(ins);
// once the data input stream is open, you can read any type of data as follows
  byte  xb = dis.readByte();  // to read a byte
  short xs = dis.readShort(); // to read a word
  int      xi = dis.readInt();     // to read an int
  

Another way would be to read the entire stream into a byte array, then open a stream from the byte array:
(Yes, you can use a byte array as the SOURCE of an input stream)

  byte[] inBuffer = new byte[256]; // define a byte array

  // open a normal input stream to a file or network connection and read 256 raw bytes into inBuffer
  // (presumably you are have code to do this)

  // then:
  ByteArrayInputStream bi = new ByteArrayInputStream(inBuffer);  
  DataInputStream dis = new DataInputStream(bi);
 
 // now use readByte, readShort, readInt, etc to read the individual items  as shown above.

There is extensive information on these methods in two books by Elliotte Rusty Harold:
Java I/O (O'REILLY) and Java Network Programming (O'REILLY)

One other thing you need to be concerned with is the byte ordering.  Java uses Big Endian and C (on a PC) uses
Little Endian.  You can find information about that in the Java I/O book.

John



"Tom Heagy" <tom.heagy@sciatl.com> wrote:
>Question:  How do I convert a byte array to an int or Integer?>
>Problem:  I'm receiving a TCP InputStream from a C application.  The stream
>consists of a byte array that was built by the C application.  Part of the
>byte array contains a number (converted to it's 4 byte format).  How do I
>take these 4 bytes and convert them into a number I can use in my Java
>application?  The String class provides a conversion constructor method for
>use with byte arrays but I can't find anything similar for numbers.
>
>Any suggestions would be greatly appreciated.
>
>-Tom
>
>
Technical Details
Message-ID: <3af6f9c1$1_1@dnews>
Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!cyclone2.usenetserver.com!news-out.usenetserver.com!dca6-feed1.news.digex.net!intermedia!cpk-news-hub1.bbnplanet.com!news.gtei.net!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews
References: <3af6e910$1_2@dnews>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,840940854720c53
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-05-07 12:56:02 PST
From: "John Pruitt" <john@branchsoft.com>
Sender: "John Pruitt" <john@branchsoft.com>
Reply-To: "John Pruitt" <john@branchsoft.com>
Subject: Re: Byte Array Conversion to Integer
Newsgroups: borland.public.jbuilder.java.language
X-User-Info: 64.242.221.201 64.242.221.201    
References:  <3af6e910$1_2@dnews> 
NNTP-Posting-Host: 207.105.83.65
Message-ID: <3af6f9c1$1_1@dnews>
Date: 7 May 2001 12:38:41 -0700
X-Trace: dnews 989264321 207.105.83.65 (7 May 2001 12:38:41 -0700)
Lines: 54
Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!cyclone2.usenetserver.com!news-out.usenetserver.com!dca6-feed1.news.digex.net!intermedia!cpk-news-hub1.bbnplanet.com!news.gtei.net!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews