def encode_ber(value, ber_length=0):
    """
    Encodes an integer to BER
    The length of the encoded BER value (in bytes) can be optionally specified
    """
    if not ber_length:
        if value < 127:
            return [value]
        elif value < 256:
            ber_length = 2
        elif value < 256 * 256:
            ber_length = 3
        elif value < 256 * 256 * 256:
            ber_length = 4
        else:
            ber_length = 5 # 32 bit unsigned int is the max for this function
    # Add the BER byte length
    ber = [127 + ber_length]
    for i in range(1, ber_length):
        ber.append( (value >> (8 * (ber_length - i - 1))) & 255 )
    return ber