Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'output': 0.05; 'bytes,': 0.09; 'literal': 0.09; 'output,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'character.': 0.16; 'escapes': 0.16; 'hex': 0.16; 'hexadecimal': 0.16; 'literal.': 0.16; 'notation': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'example': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'bytes': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'code:': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'skip:p 30': 0.29; "doesn't": 0.30; 'characters': 0.30; 'prints': 0.31; 'subject:from': 0.34; 'could': 0.34; 'module.': 0.36; 'should': 0.36; 'pm,': 0.38; 'how': 0.40; '2nd': 0.60; 'yourself,': 0.95 Date: Sun, 02 Jun 2013 18:18:07 -0400 From: Ned Batchelder User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 MIME-Version: 1.0 To: Mok-Kong Shen Subject: Re: Output from to_bytes References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370211491 news.xs4all.nl 15909 [2001:888:2000:d::a6]:38034 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46737 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 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 >