Java Utililty Methods Integer to Byte

List of utility methods to do Integer to Byte

Description

The list of methods to do Integer to Byte are organized into topic(s).

Method

byteasByte(int a)
Not defining an asByte(long) method, since asByte((int)aLong) works.
if (a != (byte) a) {
    throw new ArithmeticException("overflow: " + a);
return (byte) a;
byteint2bcdByte(int hRadix10, int lRadix10)
intbcd Byte
int bcd = ((hRadix10 & 0x0F) << 4) + (lRadix10 & 0x0F);
return (byte) bcd;
voidint2bin(byte[] out, int offset, int i)
intbin
out[offset + 0] = (byte) (i >> 24);
out[offset + 1] = (byte) (i >> 16 & 0xff);
out[offset + 2] = (byte) (i >> 8 & 0xff);
out[offset + 3] = (byte) (i & 0xff);
voidint2byte(byte[] output, int[] input, int len)
Encodes input (int) into output (bytes).
int i = 0;
if ((len % 4) != 0) {
    println("Error: int2byte(..., len), len not multiple of 4");
    println("Error: result will probably be wrong");
for (int j = 0; j < len; j += 4) {
    output[j + 0] = (byte) (input[i] & 0xff);
    output[j + 1] = (byte) ((input[i] >>> 8) & 0xff);
...
byte[]int2byte(final int i)
Convert int to byte[4]
final byte[] arr = new byte[4];
arr[0] = (byte) i;
arr[1] = (byte) (i >>> 8);
arr[2] = (byte) (i >>> 16);
arr[3] = (byte) (i >>> 24);
return arr;
byte[]int2byte(int i)
intbyte
byte dest[] = new byte[4];
dest[3] = (byte) (i & 0xff);
dest[2] = (byte) (i >>> 8 & 0xff);
dest[1] = (byte) (i >>> 16 & 0xff);
dest[0] = (byte) (i >>> 24 & 0xff);
return dest;
byte[]int2Byte(int intValue)
int Byte
byte[] b = new byte[4];
for (int i = 0; i < 4; i++) {
    b[i] = (byte) (intValue >> 8 * (3 - i) & 0xFF);
return b;
voidint2byte(int ival, byte b[], int offset)
intbyte
int bits = 32;
for (int i = 0; i < 4; i++) {
    bits -= 8;
    b[offset + i] = (byte) ((ival >> bits) & 0xff);
byte[]int2byte(int value)
Converts an integer into a byte array of hex
if (value < 0) {
    return new byte[] { (byte) (value >>> 24 & 0xFF), (byte) (value >>> 16 & 0xFF),
            (byte) (value >>> 8 & 0xFF), (byte) (value & 0xFF) };
} else if (value <= 0xFF) {
    return new byte[] { (byte) (value & 0xFF) };
} else if (value <= 0xFFFF) {
    return new byte[] { (byte) (value >>> 8 & 0xFF), (byte) (value & 0xFF) };
} else if (value <= 0xFFFFFF) {
...
byte[]int2byte(int[] ia)
intbyte
int length = ia.length;
byte[] ba = new byte[length * 4];
for (int i = 0, j = 0, k; i < length;) {
    k = ia[i++];
    ba[j++] = (byte) ((k >>> 24) & 0xFF);
    ba[j++] = (byte) ((k >>> 16) & 0xFF);
    ba[j++] = (byte) ((k >>> 8) & 0xFF);
    ba[j++] = (byte) (k & 0xFF);
...