Search code examples
pythonhexord

Multiple characters in Python ord function


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!


Solution

  • 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'])