Format ints into string of hex

前端 未结 10 1740
陌清茗
陌清茗 2020-11-30 08:16

I need to create a string of hex digits from a list of random integers (0-255). Each hex digit should be represented by two characters: 5 - \"05\", 16 - \"10\", etc.

10条回答
  •  [愿得一人]
    2020-11-30 08:51

    From Python documentation. Using the built in format() function you can specify hexadecimal base using an 'x' or 'X' Example:

    x= 255 print('the number is {:x}'.format(x))

    Output:

    the number is ff

    Here are the base options

    Type
    'b' Binary format. Outputs the number in base 2. 'c' Character. Converts the integer to the corresponding unicode character before printing. 'd' Decimal Integer. Outputs the number in base 10. 'o' Octal format. Outputs the number in base 8. 'x' Hex format. Outputs the number in base 16, using lower- case letters for the digits above 9. 'X' Hex format. Outputs the number in base 16, using upper- case letters for the digits above 9. 'n' Number. This is the same as 'd', except that it uses the current locale setting to insert the appropriate number separator characters. None The same as 'd'.

提交回复
热议问题