(python)如何将long转换为原始字符串?

时间:2015-02-15 06:24:39

标签: python

我有一段代码可以执行整数到原始字符串转换,但它不需要很长时间。任何解决方案?

def intToBytes(integer):
  hex_form = hex(integer)[2:]; # 2: gets rid of leading 0x
  if (len(hex_form) % 2):
    hex_form = '0' + hex_form;
  return bytearray.fromhex(hex_form)

1 个答案:

答案 0 :(得分:2)

您可以在出现时删除结尾'L'

def intToBytes(integer):
    hex_form = hex(integer)[2:]; # 2: gets rid of leading 0x
    if hex_form[-1:] == 'L':
        # Remove final `L` from arbitrary precision integers
        hex_form = hex_form[:-1]
    if (len(hex_form) % 2):
        hex_form = '0' + hex_form;
    return bytearray.fromhex(hex_form)