🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Byte Array Conversion to Integer

#7989
From: John Pruitt
Date: Mon, 07 May 2001 19:44
57 lines
3133 bytes
That should be: Socket s = new Socket(host,port);

"John Pruitt" <john@branchsoft.com> wrote:
>>
>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: <3af6fb2a$1_1@dnews>
Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!cyclone-sjo1.usenetserver.com!news-out-sjo.usenetserver.com!feed2.onemain.com!feed1.onemain.com!newsfeed.wirehub.nl!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnews
References: <3af6e910$1_2@dnews> <3af6f9c1$1_1@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:54:06 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>   <3af6f9c1$1_1@dnews> 
NNTP-Posting-Host: 207.105.83.65
Message-ID: <3af6fb2a$1_1@dnews>
Date: 7 May 2001 12:44:42 -0700
X-Trace: dnews 989264682 207.105.83.65 (7 May 2001 12:44:42 -0700)
Lines: 58
Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!cyclone-sjo1.usenetserver.com!news-out-sjo.usenetserver.com!feed2.onemain.com!feed1.onemain.com!newsfeed.wirehub.nl!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnews