How do I do equivelant of casting for int in python

I have a function that returns a value to me as a string. This is not an ascii representation of the value, it is the actual value, but the function returns it as a string type, while I need to treat it as an int. I am not allowed to change the function that I just have to extract from this.

in C, I would just apply it to an int pointer and resolve it as such:

myInt = *((int*)some_str_ptr)

how can i do this in python?

Again, I do not want to modify or convert the data into memory, I just want to process it and show it as an int.

update: Ive found that ord () does what I want on 1 char, but is there a built-in method to execute the whole line?

+4
source share
1

struct:

>>> struct.unpack('i', 'AAAA')[0]  # 'i' means "long" integer
                                   # 'AAAA' was the string you wanted cast to int
1094795585
>>> hex(_)
0x41414141
+7

Source: https://habr.com/ru/post/1524182/


All Articles