Conversion of int to byte and vice versa
Written by Ankur Goel
The below function is to convert an int to byte. You have to pass a int value and receive the byte in a byte array
public byte[] ToByte(int a) throws IOException
{
ByteArrayOutputStream bos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(bos);
dos.writeInt(a);
dos.flush();
byte[] b1=bos.toByteArray();
return b1;
}
The below function is to convert an byte to int. You have to pass a byte array and receive the int value
One int value = 4 byte as int size is 4 byte
public int ToInt(byte[] b) throws IOException
{
ByteArrayInputStream bis=new ByteArrayInputStream(b);
DataInputStream in=new DataInputStream(bis);
int a=in.readInt();
return a;
}
















Comments