Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #46064 > unrolled thread

Output from to_bytes

Started byMok-Kong Shen <mok-kong.shen@t-online.de>
First post2013-05-26 14:02 +0200
Last post2013-06-02 18:18 -0400
Articles 6 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Output from to_bytes Mok-Kong Shen <mok-kong.shen@t-online.de> - 2013-05-26 14:02 +0200
    Re: Output from to_bytes Chris Angelico <rosuav@gmail.com> - 2013-05-26 23:26 +1000
    Re: Output from to_bytes Terry Jan Reedy <tjreedy@udel.edu> - 2013-05-26 16:45 -0400
    Re: Output from to_bytes Grant Edwards <invalid@invalid.invalid> - 2013-05-28 15:35 +0000
      Re: Output from to_bytes Mok-Kong Shen <mok-kong.shen@t-online.de> - 2013-06-02 21:09 +0200
        Re: Output from to_bytes Ned Batchelder <ned@nedbatchelder.com> - 2013-06-02 18:18 -0400

#46064 — Output from to_bytes

FromMok-Kong Shen <mok-kong.shen@t-online.de>
Date2013-05-26 14:02 +0200
SubjectOutput from to_bytes
Message-ID<knstkh$74a$1@news.albasani.net>
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?

Thanks in advance.

M. K. Shen

[toc] | [next] | [standalone]


#46070

FromChris Angelico <rosuav@gmail.com>
Date2013-05-26 23:26 +1000
Message-ID<mailman.2174.1369574797.3114.python-list@python.org>
In reply to#46064
On Sun, May 26, 2013 at 10:02 PM, Mok-Kong Shen
<mok-kong.shen@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?

They are. If you compare them, you'll find they're identical:

>>> b'\x00\t' == b'\x00\x09'
True
>>> b'\x00\n' == b'\x00\x0a'
True

It's just a representation issue. The repr() of a bytes tries to go
for the shorter representation \n rather than the more verbose \x0a.
(Though I'm not sure why it doesn't also shorten \x00 to \0 - maybe
the \0 notation isn't deemed Pythonic, even though it does work just
fine.) So what you want is a more fixed representation.

What you may find useful here is that iterating over the bytes object
produces integers:

>>> list(b'\0\t')
[0, 9]

So you might be able to do something like this:

>>> print(''.join(('\\x%02x'%x for x in b'\0\t')))
\x00\x09

Or, in your whole loop:

>>> for k in range(8,12,1):
	print(''.join(('\\x%02x'%x for x in k.to_bytes(2,byteorder='big'))))

\x00\x08
\x00\x09
\x00\x0a
\x00\x0b

Does that help?

ChrisA

[toc] | [prev] | [next] | [standalone]


#46116

FromTerry Jan Reedy <tjreedy@udel.edu>
Date2013-05-26 16:45 -0400
Message-ID<mailman.2198.1369601157.3114.python-list@python.org>
In reply to#46064
On 5/26/2013 8:02 AM, Mok-Kong Shen wrote:
>     for k in range(8,12,1):
>       print(k.to_bytes(2,byteorder='big'))

http://bugs.python.org/issue9951
http://bugs.python.org/issue3532

import binascii as ba
for k in range(8,12,1):
     print(ba.hexlify(k.to_bytes(2,byteorder='big')))
 >>>
b'0008'
b'0009'
b'000a'
b'000b'

[toc] | [prev] | [next] | [standalone]


#46297

FromGrant Edwards <invalid@invalid.invalid>
Date2013-05-28 15:35 +0000
Message-ID<ko2irn$9oh$1@reader1.panix.com>
In reply to#46064
On 2013-05-26, Mok-Kong Shen <mok-kong.shen@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.

-- 
Grant Edwards               grant.b.edwards        Yow! I'm gliding over a
                                  at               NUCLEAR WASTE DUMP near
                              gmail.com            ATLANTA, Georgia!!

[toc] | [prev] | [next] | [standalone]


#46724

FromMok-Kong Shen <mok-kong.shen@t-online.de>
Date2013-06-02 21:09 +0200
Message-ID<kog58p$nmi$1@news.albasani.net>
In reply to#46297
Am 28.05.2013 17:35, schrieb Grant Edwards:
> On 2013-05-26, Mok-Kong Shen <mok-kong.shen@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.

M. K. Shen

[toc] | [prev] | [next] | [standalone]


#46737

FromNed Batchelder <ned@nedbatchelder.com>
Date2013-06-02 18:18 -0400
Message-ID<mailman.2565.1370211491.3114.python-list@python.org>
In reply to#46724
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@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
>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web