How do I convert a hex substring to int

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
dougconran
Posts: 27
Joined: Sat Jul 03, 2021 5:10 pm

How do I convert a hex substring to int

Post by dougconran » Wed Aug 11, 2021 10:23 pm

I have a hex string:

Code: Select all

mystr  = b'FFF7E7FF704268426803FF380E53131212121312000012BB01BA3300008200016D9E00016C790000865D00008206003723630D016E0271027103E8'
and want to get the int value of mystr[44:46] - it should come to 18

In python I would do

Code: Select all

int(mystr[44:46],base=16)
but that doesn't work in MP. I've googled around but can only find python examples.

JennaSys
Posts: 33
Joined: Tue Jul 15, 2014 8:29 am
Location: Southern California, US
Contact:

Re: How do I convert a hex substring to int

Post by JennaSys » Thu Aug 12, 2021 5:09 am

When I try

Code: Select all

int(mystr[44:46],16)
in MP v1.16 on an ESP8266 it gives me a result of 18
John Sheehan

dougconran
Posts: 27
Joined: Sat Jul 03, 2021 5:10 pm

Re: How do I convert a hex substring to int

Post by dougconran » Thu Aug 12, 2021 9:12 am

:oops: Yes, that has worked for me as well. How simple and obvious these things are when you know! - Thanks

robertcarell
Posts: 4
Joined: Thu Aug 12, 2021 10:13 pm

Re: How do I convert a hex substring to int

Post by robertcarell » Thu Aug 12, 2021 10:17 pm

dougconran wrote:
Thu Aug 12, 2021 9:12 am
:oops: Yes, that has worked for me as well. How simple and obvious these things are when you know! - Thanks
well said and that's why forums are for !!!

Post Reply