Last active
March 16, 2016 20:57
-
-
Save ksouthworth/7590056 to your computer and use it in GitHub Desktop.
Ruby code to convert an Integer to a Binary Coded Decimal (BCD) array of bytes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Convert an Integer to a "Packed" Binary Coded Decimal (BCD) formatted array of bytes | |
# http://en.wikipedia.org/wiki/Binary-coded_decimal | |
def int_to_bcd_bytes(int) | |
# BCD format takes each digit of an integer value and encodes it into a nibble (4 bits) | |
# It maintains the order of the original digits, so 15 would be 00010101 | |
# Since we're dealing with nibbles, we have to zero-pad-left if the number has an odd number of digits | |
# Decimal digit Binary | |
# 0 0000 | |
# 1 0001 | |
# 2 0010 | |
# 3 0011 | |
# 4 0100 | |
# 5 0101 | |
# 6 0110 | |
# 7 0111 | |
# 8 1000 | |
# 9 1001 | |
bcd_string = '' | |
s = int.to_s | |
s = '0' + s if s.length.odd? | |
s.chars.each do |digit| | |
bcd_string += sprintf("%04b", digit) # digit encoded as nibble | |
end | |
bcd_bytes = [bcd_string].pack("B*").unpack("C*") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
The code can be simplified. When converting an integer to string, the string you get is valid BCD. There is no need to go through the string once more before the final pack-unpack. Hence, you need to treat string characters as hex values during packing.
Command line examples: