import math


def bcd_encode(dec_value, num_octets):
    """BCD encode (little endian) an integer"""
    # Check that we have sufficent encoding space
    if math.log10(dec_value) >= (num_octets * 2):
        return ValueError("{} cannot be BCD encoded in {} octets".format(dec_value, num_octets))
    encoded = bytearray()
    for i in range(num_octets):
        low_nybble = dec_value % 10
        dec_value = int( dec_value / 10)
        high_nybble = dec_value % 10
        dec_value = int( dec_value / 10 )
        octet = (high_nybble * 16) + low_nybble
        encoded.append(octet)
    return encoded