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


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

Re: python b'...' notation

Started byCameron Simpson <cs@zip.com.au>
First post2013-05-30 08:19 +1000
Last post2013-05-31 00:51 -0700
Articles 3 — 3 participants

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: python b'...' notation Cameron Simpson <cs@zip.com.au> - 2013-05-30 08:19 +1000
    Re: python b'...' notation alcyon <steve@terrafirma.us> - 2013-05-30 15:19 -0700
      Re: python b'...' notation jmfauth <wxjmfauth@gmail.com> - 2013-05-31 00:51 -0700

#46417 — Re: python b'...' notation

FromCameron Simpson <cs@zip.com.au>
Date2013-05-30 08:19 +1000
SubjectRe: python b'...' notation
Message-ID<mailman.2384.1369866008.3114.python-list@python.org>
On 29May2013 13:14, Ian Kelly <ian.g.kelly@gmail.com> wrote:
| On Wed, May 29, 2013 at 12:33 PM, alcyon <steve@terrafirma.us> wrote:
| > This notation displays hex values except when they are 'printable', in which case it displays that printable character.  How do I get it to force hex for all bytes?  Thanks, Steve
| 
| Is this what you want?
| 
| >>> ''.join('%02x' % x for x in b'hello world')
| '68656c6c6f20776f726c64'

Not to forget binascii.hexlify.
-- 
Cameron Simpson <cs@zip.com.au>

Every particle continues in its state of rest or uniform motion in a straight
line except insofar as it doesn't.      - Sir Arther Eddington

[toc] | [next] | [standalone]


#46549

Fromalcyon <steve@terrafirma.us>
Date2013-05-30 15:19 -0700
Message-ID<94eb93eb-3f95-448c-979a-5bfc4da9ef06@googlegroups.com>
In reply to#46417
On Wednesday, May 29, 2013 3:19:42 PM UTC-7, Cameron Simpson wrote:
> On 29May2013 13:14, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> 
> | On Wed, May 29, 2013 at 12:33 PM, alcyon <steve@terrafirma.us> wrote:
> 
> | > This notation displays hex values except when they are 'printable', in which case it displays that printable character.  How do I get it to force hex for all bytes?  Thanks, Steve
> 
> | 
> 
> | Is this what you want?
> 
> | 
> 
> | >>> ''.join('%02x' % x for x in b'hello world')
> 
> | '68656c6c6f20776f726c64'
> 
> 
> 
> Not to forget binascii.hexlify.
> 
> -- 
> 
> Cameron Simpson <cs@zip.com.au>
> 
> 
> 
> Every particle continues in its state of rest or uniform motion in a straight
> 
> line except insofar as it doesn't.      - Sir Arther Eddington

Thanks for the binascii.hexlify tip. I was able to make it work but I did have to write a function to get it exactly the string I wanted.  I wanted, for example, <b'\n\x00'> to display as <0x0A 0x00> or <b'!\xff(\xc0'> to display as <0x21 0xFF 0x28 0xC0>.  

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


#46579

Fromjmfauth <wxjmfauth@gmail.com>
Date2013-05-31 00:51 -0700
Message-ID<bf0cc4f9-5535-4174-b0e3-e265701c0e9b@k3g2000vbn.googlegroups.com>
In reply to#46549
On 31 mai, 00:19, alcyon <st...@terrafirma.us> wrote:
> On Wednesday, May 29, 2013 3:19:42 PM UTC-7, Cameron Simpson wrote:
> > On 29May2013 13:14, Ian Kelly <ian.g.ke...@gmail.com> wrote:
>
> > | On Wed, May 29, 2013 at 12:33 PM, alcyon <st...@terrafirma.us> wrote:
>
> > | > This notation displays hex values except when they are 'printable', in which case it displays that printable character.  How do I get it to force hex for all bytes?  Thanks, Steve
>
> > |
>
> > | Is this what you want?
>
> > |
>
> > | >>> ''.join('%02x' % x for x in b'hello world')
>
> > | '68656c6c6f20776f726c64'
>
> > Not to forget binascii.hexlify.
>
> > --
>
> > Cameron Simpson <c...@zip.com.au>
>
> > Every particle continues in its state of rest or uniform motion in a straight
>
> > line except insofar as it doesn't.      - Sir Arther Eddington
>
> Thanks for the binascii.hexlify tip. I was able to make it work but I did have to write a function to get it exactly the string I wanted.  I wanted, for example, <b'\n\x00'> to display as <0x0A 0x00> or <b'!\xff(\xc0'> to display as <0x21 0xFF 0x28 0xC0>.

--------

>>> a = b'!\xff(\xc0\n\x00'
>>> z = ['0x{:02X}'.format(c) for c in b]
>>> z
['0x21', '0xFF', '0x28', '0xC0', '0x0A', '0x00']
>>> s = ' '.join(z)
>>> s
'0x21 0xFF 0x28 0xC0 0x0A 0x00'
>>>

jmf

[toc] | [prev] | [standalone]


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


csiph-web