Skip to content

Commit 6b7b039

Browse files
committed
fix: mc length and hex processing with tests
1 parent 5611a49 commit 6b7b039

3 files changed

Lines changed: 156 additions & 56 deletions

File tree

sd_protocols/helpers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,24 @@ def length_in_range(self, protocol_id, message_length):
150150

151151
return (1, '')
152152

153+
def hex_to_bin_str(self, hex_string):
154+
"""
155+
Convert hex string to binary string.
156+
157+
Args:
158+
hex_string: Hexadecimal string (e.g., '1A3F')
159+
160+
Returns:
161+
Binary string (e.g., '0001101000111111') or None if input is invalid
162+
"""
163+
if hex_string is None:
164+
return None
165+
166+
try:
167+
# Convert hex to integer, then format as binary
168+
bin_string = bin(int(hex_string, 16))[2:] # Remove '0b' prefix
169+
# Pad with leading zeros to make length a multiple of 4
170+
padded_length = ((len(bin_string) + 3) // 4) * 4
171+
return bin_string.zfill(padded_length)
172+
except ValueError:
173+
return None

0 commit comments

Comments
 (0)