The Artima Developer Community
Sponsored Link

Java Answers Forum
convert an int into a byte array

7 replies on 1 page. Most recent reply: Jun 6, 2004 11:39 PM by Philip Foulkes

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
Courtenay

Posts: 23
Nickname: courtz
Registered: Apr, 2003

convert an int into a byte array Posted: Mar 23, 2004 11:27 AM
Reply to this message Reply
Advertisement
Can anyone help? i need a method to convert an int into a byte array and vice versa? thanks!


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: convert an int into a byte array Posted: Mar 23, 2004 12:21 PM
Reply to this message Reply
I don't really understand what an int and a byte array have to do with one another. What mausam showed you DOES take data from an int and put it in a byte array. It just uses a String as an intermediate step.

So if you start with an int of 274, you get a byte array with the first element being 2, the second being 7, and the third being 4. If that isn't what you need to do, then you need to explain the goal in more detail.

By itself, the phrase "convert an int into a byte array" has no meaning.

twc

Courtenay

Posts: 23
Nickname: courtz
Registered: Apr, 2003

Re: convert an int into a byte array Posted: Mar 24, 2004 9:53 AM
Reply to this message Reply
My end goal is for it to be transmitted over a network. this is the question posed to us:
We need to be able to send more advanced structures over the network. How can we convert these structures to the byte arrays supported by the networking classes?
How is an int represented in Java? Specifically: How many bytes are used to represent an int? What is the relevance of each byte? How can an int be converted into a byte array?
Write a method to convert an int into a byte array.
Write a method that converts the byte array back into an int.
Write a method that returns the size of the byte array needed to store an int.

Courtenay

Posts: 23
Nickname: courtz
Registered: Apr, 2003

Re: convert an int into a byte array Posted: Mar 24, 2004 9:54 AM
Reply to this message Reply
My end goal is for it to be transmitted over a network. this is the question posed to us:
We need to be able to send more advanced structures over the network. How can we convert these structures to the byte arrays supported by the networking classes?
How is an int represented in Java? Specifically: How many bytes are used to represent an int? What is the relevance of each byte? How can an int be converted into a byte array?
Write a method to convert an int into a byte array.
Write a method that converts the byte array back into an int.
Write a method that returns the size of the byte array needed to store an int.

WEW

Posts: 2
Nickname: wew
Registered: Apr, 2004

Re: convert an int into a byte array Posted: Apr 2, 2004 2:50 PM
Reply to this message Reply
I have been working on transmitting JPEG's through sockets.
In order to read each picture, I needed to know the number of bytes. I too had to create int to byte[] and byte[] to int.
Here is the code:

/**
* @param ba
* @return
*/
private int getIntFromByteArray(byte[] ba) {
int i;
if (ba[0] < 0)
i = ba[0] + 256;
else
i = ba[0];
i <<= 8;
if (ba[1] < 0)
i += ba[1] + 256;
else
i += ba[1];
i <<= 8;
if (ba[2] < 0)
i += ba[2] + 256;
else
i += ba[2];
i <<= 8;
if (ba[3] < 0)
i += ba[3] + 256;
else
i += ba[3];
return i;

}
/**
* @param i
* @return
*/
private byte[] getIntToByteArray(int v) {

byte b[] = new byte[4];
int i, shift;

for(i = 0, shift = 24; i < 4; i++, shift -= 8)
b[i] = (byte)(0xFF & (v >> shift));

return b;


}

WEW

Posts: 2
Nickname: wew
Registered: Apr, 2004

Re: convert an int into a byte array Posted: Apr 2, 2004 2:51 PM
Reply to this message Reply
for whatever reason the code tags don't work

and for homework you will have to figure out where the
array [] are in the array types.

:)

Mike

Posts: 1
Nickname: ak140
Registered: Jun, 2004

Re: convert an int into a byte array Posted: Jun 6, 2004 3:45 PM
Reply to this message Reply
Well you could just use bit shifts to place the proper bits into the byte array.

4 byte INT : 0000 0001 - 0110 0000 - 0010 0100 - 0011 0000
byte array :[0000 0001][0110 0000][0010 0100][0011 0000]
index array: -----0------------1------------2------------3
byte value : -----1------------96-----------36-----------48

So all you have to do is handle it 1 byte at a time
and then bit shift the int by 1 byte and extract the next byte.

The following method writes myInt into the buffer starting at the given offset. This function works backwards by finding the values of the right most bits first and then moving left.

public void writeInt(int myInt, byte[] buffer, int off){
for(int i = 0; i < 4; i++){
buffer[off + (3 - i)] = (byte)(myInt >> 8 * i & 0xFF);
}
}

to decode the buffer

public int readInt(byte[] buffer, int offset){
int n = 0;
for(int i = 0; i < 4; i++){
n += ((0xFF & buffer[offset + i]) << 8 * (3 - i));
}
return n;
}

Philip Foulkes

Posts: 19
Nickname: frodo
Registered: May, 2004

Re: convert an int into a byte array Posted: Jun 6, 2004 11:39 PM
Reply to this message Reply
When posting java code, use java the java tags, not code tags.

Flat View: This topic has 7 replies on 1 page
Topic: Someone educate me... Previous Topic   Next Topic Topic: Dynamic class reloading

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use