0

I am having an integer value.Now first i convert it to a byte array of size 4 by following logic :

LOGIC 1

byte[] tempbytes = new byte[4]; int y=1; for(i=3;i>=0;i--){ int sum=0; for(int j=0;j<8;j++){ int z=x & y; x=x>>1; sum=sum+power(2, j)*z; } tempbytes[i]=(byte)sum; } 

LOGIC 2 :

 int sizeOffile; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeInt(sizeOfFile); dos.close(); byte[] tempbytes = baos.toByteArray(); 

Now this array may contain negative values too like if int value is 8401 then corresponding byte array is 0 0 32 -47.

I then convert this byte array to a string say S by doing something like this :

S=new String(temp); 

I then convert back this String to bytes by using getBytes().

But the result now it gives are 0 0 32 -17.So when i convert it back to int it gives wrong result. Here sizefile is bytearray after i convert String to bytes again.

int sizeoffilerecieved = java.nio.ByteBuffer.wrap(sizefile).getInt(); 

What can be problem here.Please help how to solve it.

5
  • 1
    Square all your outputs and you will always get positive numbers Commented Mar 21, 2014 at 11:14
  • it is not clear what you try to d, but with sum=sum+power(2, j)*z; you create bigget numbers every time, to the power of something bigger. So eventually you overflow byte when is casted from an int (sum) Commented Mar 21, 2014 at 11:18
  • 1
    And what is that logic? It is unclear from the code Commented Mar 21, 2014 at 11:20
  • @Avempace Assume that it is logic just to convert an int to byte array.Which is tested correct Commented Mar 21, 2014 at 11:23
  • I don't understand the idea behind your code. Are you trying to get the bit-representation of the integer value, where the first 8 bit are stored in tempbyte[0], second 8 bit in tempbyte[1] etc.? What is the idea behind the task? Where do you define the variable x? Commented Mar 21, 2014 at 11:54

2 Answers 2

2

Assuming your convertion from int to byte array is correct, negative bytes are normal because that's their decimal representation, it's not an error. If the byte value is over 127, the MSB will be 1, which for Java means a negative number. In Java there's no unsigned values, so if you show as decimal representation it will always show negative. If you want to see the byte value unsigned you can convert to hex representation (Integer.toHexString), or use a bigger holder like short or int to see the unsigned decimal value.

The point here is that the byte value is always the same, but you can show it in different ways (signed, unsigned, hexadecimal, etc..). The value representation can confuse you. The value and its representation are two different things.

Also you might want to look into bit shifting for this instead of using powers, which is way slower.

Sign up to request clarification or add additional context in comments.

6 Comments

ididnt get you .please elaborate
If you didn't understand anything I said, I sugegst you look into number representations. If you didn't understand something specific in my answer, please tell me so I can explain further.
where is toHexString ?
Sorry, there's no Byte.toHexString. You can use Integer.toHexString instead.
Actually i need to convert the int to byte array then do some work on byte array then convert it to string.Then send it to other person as string then convert back to byte array and then back to int
|
0

It works, method takes integer value and convert 4 byte array..

 byte[] IntTo4ByteArray( int data ) { byte[] byteResult = new byte[4]; byteResult[0] = (byte) ((data & 0xFF000000) >> 24); byteResult[1] = (byte) ((data & 0x00FF0000) >> 16); byteResult[2] = (byte) ((data & 0x0000FF00) >> 8); byteResult[3] = (byte) ((data & 0x000000FF) >> 0); return byteResult; } 

or

 int data; byte[] byteResult = new byte[4] ; ByteBuffer.wrap(byteResult).putInt(data); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.