translating foreign data

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Jun 23 23:23:22 EDT 2018


On Sun, 24 Jun 2018 12:53:49 +1000, Chris Angelico wrote:

[...]
>> Okay, you want a bit-pattern. In hex:
>>
>> '0x313030e282ac'
[...]

> Hmm. Actually, I'm a bit confused.
> 
>>>> hex("100€".encode())
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: 'bytes' object cannot be interpreted as an integer
> 
> Nope, that's not it. Needs something to turn the bytes into an integer
> first. But I can't find a way to do that. Best I can find is:
> 
>>>> "100€".encode().hex()
> '313030e282ac'

Dammit, that was what I was looking for, but I only looked on *strings*, 
not bytes.

 
> No "0x" prefix, no function call. So, I'm stuck. How did you create your
> one?

py> hex(int.from_bytes("100€".encode("utf-8"), 'big'))
'0x313030e282ac'



-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson




More information about the Python-list mailing list