def METHOD_NAME(r):
    assert r >= 0  # can't support negative numbers yet
    h = ("%x" % r).encode()
    if len(h) % 2:
        h = b'0' + h
    s = binascii.unhexlify(h)
    num = s[0] if isinstance(s[0], int) else ord(s[0])
    if num <= 0x7f:
        return b'\x02' + int.to_bytes(len(s), 1, 'big') + s
    else:
        # DER integers are two's complement, so if the first byte is
        # 0x80-0xff then we need an extra 0x00 byte to prevent it from
        # looking negative.
        return b'\x02' + int.to_bytes(len(s)+1, 1, 'big') + b'\x00' + s