Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.019 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'output': 0.05; 'tries': 0.07; '[0,': 0.09; 'deemed': 0.09; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'help?': 0.16; 'iterating': 0.16; 'notation': 0.16; 'repr()': 0.16; 'shorten': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'issue.': 0.22; 'bytes': 0.24; 'compare': 0.26; 'this:': 0.26; 'code:': 0.26; 'gets': 0.27; 'header:In-Reply- To:1': 0.27; 'fixed': 0.29; 'skip:p 30': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'are.': 0.31; 'produces': 0.31; 'maybe': 0.34; 'subject:from': 0.34; 'could': 0.34; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'shorter': 0.36; 'useful': 0.36; 'should': 0.36; 'received:209': 0.37; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'even': 0.60; '2nd': 0.60; "you'll": 0.62; 'more': 0.64; 'here': 0.66; '26,': 0.68; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=xd4CQbbWfjWIMLqCYDRhxJn4GXL1IKjhUMZdopb+KsY=; b=cWf2Rf11x/kHRdC4xgm6Xfw5F9mxo1bfy6yHzBk9p/2X0++86lUtQYKiFOt5HfeRGL QyT9hquyeB1XyERNT3mU9KRSHprdpaF/nuULHWJSZFx6YXkip8VsRKvNRY7s22fcafUR zx6l/Oyic1ZOffE+CiHV5AbMCwRiWoiKsxXWb4yd6aMAcrVjksUdAT+Zd3X7bmd9dlz1 jeI86UBy0odkr1CI2A1iLtrRdDsIUtjTaCBHQEnM+FP8HB4RXCc0biziGRviaVNVpku5 LTWdTfdtfQSPoHjkU3tTqTbDX2AREILeT93ePj2k7/CfsRUcwIwExidnUdyBl3v1ZZy7 hstw== MIME-Version: 1.0 X-Received: by 10.52.121.99 with SMTP id lj3mr2037024vdb.52.1369574793841; Sun, 26 May 2013 06:26:33 -0700 (PDT) In-Reply-To: References: Date: Sun, 26 May 2013 23:26:33 +1000 Subject: Re: Output from to_bytes From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369574797 news.xs4all.nl 15955 [2001:888:2000:d::a6]:52490 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46070 On Sun, May 26, 2013 at 10:02 PM, Mok-Kong Shen 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