Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: get int value from a char string

Tags:

python

char

ord

chr

This is one of those silly questions and I don't really know how to formulate it, so I'll give an example. I got

v = chr(0xae) + chr(0xae)

where #AEAE is, in decimal, the value of 44718.

My question is how I get the integer value of v? I know about ord() but I can use it only for a char, and not for a string.

Thank you.

like image 935
ov1d1u Avatar asked Mar 25 '23 01:03

ov1d1u


1 Answers

I managed to do this using the struct module:

import struct
int_no = struct.unpack('>H', v)[0]
print int_no

which outputs the desired results:

44718
like image 152
ov1d1u Avatar answered Apr 19 '23 23:04

ov1d1u