Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple characters in Python ord function

Tags:

python

hex

ord

Programming beginner here. (Python 2.7)

Is there a work around for using more than a single character for Python's ord function?

For example, I have a hex string '\xff\x1a' which I'd like the decimal value for so that I can sum it with other hex strings. However ord only accepts a single hex string character.

Thank you!

like image 834
micuzzo Avatar asked Jan 28 '15 00:01

micuzzo


1 Answers

Strings are iterable, so you can loop through the string, use ord and add the results:

your_sum = sum([ord(i) for i in '\xff\x1a'])
like image 164
Malik Brahimi Avatar answered Oct 13 '22 00:10

Malik Brahimi