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.
tempbyte[0], second 8 bit intempbyte[1]etc.? What is the idea behind the task? Where do you define the variablex?