Add binary converter to Utils
This commit is contained in:
1 file changed
+20
@@ -76,6 +76,16 @@ public class Utils {
|
||||
"F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7",
|
||||
"F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF"};
|
||||
|
||||
private static final String[] BIN_CONSTANTS = new String[256];
|
||||
|
||||
static {
|
||||
for (int i = 0; i < 256; i++) {
|
||||
String bin = Integer.toBinaryString(i);
|
||||
// Left-pad to 8 digits with zeros
|
||||
BIN_CONSTANTS[i] = "00000000".substring(bin.length()) + bin;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Very fast 8-bit int to ASCII conversion.
|
||||
*
|
||||
@@ -116,4 +126,14 @@ public class Utils {
|
||||
public static int address(int lowByte, int hiByte) {
|
||||
return ((hiByte << 8) | lowByte) & 0xffff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Very fast 8-bit int to binary conversion, with zero-padded output.
|
||||
*
|
||||
* @param val The unsigned 8-bit value to convert to a zero padded binary string.
|
||||
* @return Eight digit, zero padded binary string.
|
||||
*/
|
||||
public static String byteToBin(int val) {
|
||||
return BIN_CONSTANTS[val & 0xff];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user