0

I am trying to store an i2c address, 0x3c, to a string to be stored in a text file that is later read. When reading the text file however, I cannot read the data from the string in the correct way, such that

value = string_read(text_file)
print(value == 0x3c)

would return true. How can I read a single byte stored in a string:

'0x3c'

into value so that the above code would return true?

Cadell
  • 39
  • 5

1 Answers1

3

See: https://stackoverflow.com/a/209550/9606335. Specifically, in your example, if you know your string is only "0x3c", then you can convert it to a numerical value by value = int("0x3c", 0). Now your expression should behave as you expect:

>>> print(int("0x3c", 0) == 0x3c)
True
Athena
  • 320
  • 2
  • 12