1

I have a similar questions as this one: I have strings which contain hex values such as "0x8F" which I need to convert to hex. Using the previous answer and doing

int("0x8F", 16)

works, but the result is displayed as a regular int (143 in the example).

Is it possible to keep the format as hex after conversion??

silent
  • 14,494
  • 4
  • 46
  • 86
  • 1
    Are you trying to output them as hex? just go print(hex(intVal)) https://stackoverflow.com/questions/2269827/how-to-convert-an-int-to-a-hex-string – anand_v.singh Apr 02 '19 at 10:28
  • What does is it mean "keep the format as hex"? If it means "keep the original string", well, you already have it. – Reut Sharabani Apr 02 '19 at 10:29
  • Possible duplicate of [How to convert an int to a hex string?](https://stackoverflow.com/questions/2269827/how-to-convert-an-int-to-a-hex-string) – anand_v.singh Apr 02 '19 at 10:32
  • @silent when I say you already have the string I mean you can just keep it and output it when you need to. No need for extra code. Use the integer representation where needed (comparisons etc.) and the string representation where needed (printing). – Reut Sharabani Apr 02 '19 at 10:33

1 Answers1

1

You can use format to get hex formatting of a number. Check out the docs for format here.

This will print out the value you want:

print(format(143, '#X'))
fulaphex
  • 2,879
  • 3
  • 19
  • 26