From e635d0a60819067f211eaaf233a8dfed296f1e02 Mon Sep 17 00:00:00 2001 From: Rui Barbosa Date: Sat, 26 Sep 2026 19:46:54 +0100 Subject: [PATCH] Add binary converter to Utils --- .../java/com/loomcom/symon/util/Utils.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/com/loomcom/symon/util/Utils.java b/src/main/java/com/loomcom/symon/util/Utils.java index 48d2a15..ae5c819 100644 --- a/src/main/java/com/loomcom/symon/util/Utils.java +++ b/src/main/java/com/loomcom/symon/util/Utils.java @@ -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]; + } } \ No newline at end of file