0

How to convert hexadecimal value 0x000F into string value "0F" in python? Basically, I've been trying to convert number to string.

temoto
  • 5,394
  • 3
  • 34
  • 50
Pyou
  • 13
  • 2
  • This question might be similar to the following questions: https://stackoverflow.com/questions/209513/convert-hex-string-to-int-in-python – hepidad Aug 02 '19 at 22:02
  • Is your input a string like `"0x000F"`, or is it an integer with that value? – Barmar Aug 02 '19 at 22:10
  • its not a string its a hex value 0x000F and I want to convert it in to string "0F" I do not want to convert to int or anything else . – Pyou Aug 02 '19 at 22:14
  • There really is no such thing as a "hex value". There are just ints, with hex being one possible form that an int literal can take. You can use `hex(i)` and tweak the resulting string. – John Coleman Aug 02 '19 at 22:15

1 Answers1

3

Use % formatting:

str = "%02x" % 0x000F

%x formatting means to format as a float, the 02 prefix means to put it in a 2-digit field with zero padding.

Barmar
  • 741,623
  • 53
  • 500
  • 612