translating foreign data

Chris Angelico chris.angelico at 1
Sun Jun 24 14:32:20 EDT 2018


From: Chris Angelico <rosuav at gmail.com>

On Sun, Jun 24, 2018 at 1:23 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> 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'

Ahhh thanks, that's the part I couldn't find (and didn't remember).

Anyhow, encoding to UTF-8 and then to bytes is pretty easy.

ChrisA

--- BBBS/Li6 v4.10 Toy-3
 * Origin: Prism bbs (1:261/38)



More information about the Python-list mailing list