Output from to_bytes

Ned Batchelder ned at nedbatchelder.com
Sun Jun 2 18:18:07 EDT 2013


On 6/2/2013 3:09 PM, Mok-Kong Shen wrote:
> Am 28.05.2013 17:35, schrieb Grant Edwards:
>> On 2013-05-26, Mok-Kong Shen <mok-kong.shen at t-online.de> wrote:
>>> I don't understand why with the code:
>>>
>>>      for k in range(8,12,1):
>>>        print(k.to_bytes(2,byteorder='big'))
>>>
>>> one gets the following output:
>>>
>>>      b'\x00\x08'
>>>      b'\x00\t'
>>>      b'\x00\n'
>>>      b'\x00\x0b'
>>>
>>> I mean the 2nd and 3rd should be b'\x00\x09' and b'x00\x0a'.
>>> Anyway, how could I get the output in the forms I want?
>>
>> Well, it would help if you told us what output form you want.
>
> As I stated, I like the 2nd and 3rd be b'\x00\x09' and b'\x00\x0a'
> respectively. This is what would expeacted to be in a hexadecimal
> notation IMHO in other PLs.
>

When you print bytes, Python doesn't use "hexadecimal notation."  It 
prints a Python bytes literal.  That literal will use printable 
characters like 'Q', or hex escapes like '\x00', or other escapes like 
'\n', depending on the character.   If you want hex output, you have to 
create it yourself, for example with the binascii module.

--Ned.

> M. K. Shen
>




More information about the Python-list mailing list