Author: Brian A. Ree
0: Java Byte Converter: Intro
The java bit converter tutorial covers a helpful support class that allows you to convert different java base
types to byte arrays and vice versa. This is extremely useful in socket servers and other binary applications like file
encodings.
1: Java Byte Converter: Integer
public static final byte[] toByte(int i, boolean flag) {
byte abyte0[] = new byte[4];
for (byte byte0 = 0; byte0 <= 3; byte0++) {
abyte0[byte0] = (byte) (i >>> (3 - byte0) * 8);
}
if (flag) {
abyte0 = reverse_order(abyte0, 4);
}
return abyte0;
}
public static final byte[] toByte(int i) {
return toByte(i, false);
}
public static final int toInt(byte abyte0[], boolean flag) {
int i = 0;
if (flag) {
abyte0 = reverse_order(abyte0, 4);
}
for (byte byte0 = 0; byte0 <= 3; byte0++) {
int j;
if (abyte0[byte0] < 0) {
abyte0[byte0] = (byte) (abyte0[byte0] & 0x7f);
j = abyte0[byte0];
j |= 0x80;
} else {
j = abyte0[byte0];
}
i |= j;
if (byte0 < 3) {
i <<= 8;
}
}
return i;
}
public static final int toInt(byte abyte0[]) {
return toInt(abyte0, false);
}
Above are listed the integer conversion methods. The toByte method converts an integer into a byte array.
The method is overloaded with one direction being the default and a reverse direction attainable via a boolean argument.
The array is created by bit shifting the value of the integer based on a loop index which represents the offset.
Similarly there are two toInt methods. It is also overloaded to enforce a default direction when no boolean parameter is passed in.
2: Java Byte Converter: Short
public static final byte[] toByte(short word0, boolean flag) {
byte abyte0[] = new byte[2];
for (byte byte0 = 0; byte0 <= 1; byte0++) {
abyte0[byte0] = (byte) (word0 >>> (1 - byte0) * 8);
}
if (flag) {
abyte0 = reverse_order(abyte0, 2);
}
return abyte0;
}
public static final byte[] toByte(short word0) {
return toByte(word0, false);
}
public static final short toShort(byte abyte0[], boolean flag) {
short word0 = 0;
if (flag) {
abyte0 = reverse_order(abyte0, 2);
}
for (byte byte0 = 0; byte0 <= 1; byte0++) {
short word1;
if (abyte0[byte0] < 0) {
abyte0[byte0] = (byte) (abyte0[byte0] & 0x7f);
word1 = abyte0[byte0];
word1 |= 0x80;
} else {
word1 = abyte0[byte0];
}
word0 |= word1;
if (byte0 < 1) {
word0 <<= 8;
}
}
return word0;
}
public static final short toShort(byte abyte0[]) {
return toShort(abyte0, false);
}
Above are listed the short conversion methods. The toByte method converts a short into a byte array.
The method is overloaded with one direction being the default and a reverse direction attainable via a boolean argument.
The array is created by bit shifting the value of the short based on a loop index which represents the offset.
Similarly there are two toShort methods. It is also overloaded to enforce a default direction when no boolean parameter is passed in.
3: Java Byte Converter: Long
public static final byte[] toByte(long l, boolean flag) {
byte abyte0[] = new byte[8];
for (byte byte0 = 0; byte0 <= 7; byte0++) {
abyte0[byte0] = (byte) (int) (l >>> (7 - byte0) * 8);
}
if (flag) {
abyte0 = reverse_order(abyte0, 8);
}
return abyte0;
}
public static final byte[] toByte(long l) {
return toByte(l, false);
}
public static final long toLong(byte abyte0[], boolean flag) {
long l = 0L;
if (flag) {
abyte0 = reverse_order(abyte0, 8);
}
for (byte byte0 = 0; byte0 <= 7; byte0++) {
long l1;
if (abyte0[byte0] < 0) {
abyte0[byte0] = (byte) (abyte0[byte0] & 0x7f);
l1 = abyte0[byte0];
l1 |= 128L;
} else {
l1 = abyte0[byte0];
}
l |= l1;
if (byte0 < 7) {
l <<= 8;
}
}
return l;
}
public static final long toLong(byte abyte0[]) {
return toLong(abyte0, false);
}
I guess you're seeing a pattern by now? The long conversion methods are almost identical to the short and integer conversion methods but with a few more iterations
to handle the extra bytes. We'll go through the next foew methods a bit faster.
4: Java Byte Converter: Char
public static final byte[] toByte(char c, boolean flag) {
byte abyte0[] = new byte[2];
for (byte byte0 = 0; byte0 <= 1; byte0++) {
abyte0[byte0] = (byte) (c >>> (1 - byte0) * 8);
}
if (flag) {
abyte0 = reverse_order(abyte0, 2);
}
return abyte0;
}
public static final byte[] toByte(char c) {
return toByte(c, false);
}
public static final char toChar(byte abyte0[], boolean flag) {
char c = '\0';
if (flag) {
abyte0 = reverse_order(abyte0, 2);
}
c = (char) ((c | (char) abyte0[0]) << 8);
c |= (char) abyte0[1];
return c;
}
public static final char toChar(byte abyte0[]) {
return toChar(abyte0, false);
}
5: Java Byte Converter: Float
public static final byte[] toByte(float f, boolean flag) {
byte abyte0[] = new byte[4];
Float float1 = new Float(f);
int i = Float.floatToIntBits(f);
abyte0 = toByte(i, flag);
return abyte0;
}
public static final byte[] toByte(float f) {
return toByte(f, false);
}
public static final float toFloat(byte abyte0[], boolean flag) {
float f = 0.0F;
Float float1 = new Float(f);
int i = toInt(abyte0, flag);
f = Float.intBitsToFloat(i);
return f;
}
public static final float toFloat(byte abyte0[]) {
return toFloat(abyte0, false);
}
6: Java Byte Converter: Double
public static final byte[] toByte(double d, boolean flag) {
byte abyte0[] = new byte[8];
Double double1 = new Double(d);
long l = Double.doubleToLongBits(d);
abyte0 = toByte(l, flag);
return abyte0;
}
public static final byte[] toByte(double d) {
return toByte(d, false);
}
public static final double toDouble(byte abyte0[], boolean flag) {
double d = 0.0D;
Double double1 = new Double(d);
long l = toLong(abyte0, flag);
d = Double.longBitsToDouble(l);
return d;
}
public static final double toDouble(byte abyte0[]) {
return toDouble(abyte0, false);
}
private static byte[] reverse_order(byte abyte0[], int i) {
byte abyte1[] = new byte[i];
for (byte byte0 = 0; byte0 <= i - 1; byte0++) {
abyte1[byte0] = abyte0[i - 1 - byte0];
}
return abyte1;
}
Well I hope you've enjoyed this little crash course into binary data in java. There are newer ways to do what we've done here but these methods will do the trick for you in
a pinch. They are great for socket servers and other binary encoding tasks you may come across in java.