[Python] Conversione long to hex

Carlo Miron miron a python.it
Mar 28 Nov 2017 18:15:44 CET


2017-11-28 17:30 GMT+01:00 Riccardo Magliocchetti
<riccardo.magliocchetti a gmail.com>:

> Il 28/11/2017 17:15, Massimiliano Rosi ha scritto:
>>
>> Buonasera a tutti,
>>
>> ho un dubbio riguardo alla conversione di un long verso hex con il metodo
>> to_bytes, perché ritorna un risultato decisamente molto strano:
>>
>> il numero che devo convertire presenta un'anomalia che non riesco a capire
>>
>>  >>> f = 200000100
>>  >>> (f).to_bytes(4, byteorder = 'big')
>> b'\x0b\xeb\xc2d'
>>
>> che non mi sembra una corretta rappresentazione esadecimale.
>>
>> il valore corretto dovrebbe essere: \x0b\xeb\xc2\x64
>>
>> Riuscite ad aiutarmi a capire il perché?
>
>
> Ma hai provato a scriverlo su file perchè magari è solo una questione di
> rappresentazione?

sembrerebbe di sì, perché

>>> hex(200000100)
'0xbebc264'
>>> (200000100).to_bytes(4, byteorder = 'big')
b'\x0b\xeb\xc2d'
>>> for i in (200000100).to_bytes(4, byteorder = 'big'): print(hex(i), end=' ')
...
0xb 0xeb 0xc2 0x64 >>>

HTH,
㎝

-- 
|:**THE 🍺-WARE LICENSE** *(Revision ㊷)*:
| <miron@🐍.it> wrote this mail. As long as you retain this
| notice you can do whatever you want with this stuff.
| If we meet some day, and you think this stuff is worth it,
| you can buy me a 🍺 in return. —㎝


Maggiori informazioni sulla lista Python