Path: csiph.com!usenet.pasdenom.info!goblin3!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed6.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.017 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'subject:data': 0.07; 'character,': 0.09; 'continuation': 0.09; 'messing': 0.09; 'pretend': 0.09; 'subject:string': 0.09; 'utf-8': 0.09; 'python': 0.11; 'assume': 0.12; '>the': 0.16; 'ascii': 0.16; 'encoding.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'subject:changing': 0.16; 'sure.': 0.16; 'wrote:': 0.21; 'header:In-Reply-To:1': 0.22; 'header:User- Agent:1': 0.23; 'convert': 0.23; 'that.': 0.28; 'operations.': 0.29; 'safely': 0.29; 'vice': 0.29; 'asking': 0.29; 'character': 0.30; 'chris': 0.32; 'subject: (': 0.33; 'byte': 0.33; 'bytes': 0.33; 'could': 0.34; 'there': 0.35; 'received:84': 0.35; 'things': 0.36; 'subject:)': 0.36; 'but': 0.36; 'does': 0.36; 'next': 0.38; 'something': 0.38; 'goes': 0.38; 'correct': 0.38; 'being': 0.39; "can't": 0.39; 'to:addr:python-list': 0.39; 'think': 0.40; 'to:addr:python.org': 0.40; 'single': 0.61; 'more': 0.63; 'strings': 0.66; 'understood': 0.66; 'header:Reply-To:1': 0.66; 'direct': 0.70; 'reply-to:no real name:2**0': 0.72; '256': 0.84; 'encoding,': 0.84; 'recreate': 0.84; 'reply-to:addr:python.org': 0.84; 'such,': 0.84; 'summarized': 0.84; 'technically': 0.84; 'encounter': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=Dp/UCRD+ c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=rXS5J2IKs6wA:10 a=A37g47N46L0A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=cUb0p1ca0q5EZsskVAYA:9 a=V5qsgsLWQ1GW0bLcLkQA:7 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Wed, 28 Mar 2012 20:50:01 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20120312 Thunderbird/11.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: "convert" string to bytes without changing data (encoding) References: <9tg21lFmo3U1@mid.dfncis.de> <4f73504c$0$29981$c3e8da3$5496439d@news.astraweb.com> <5B80DD153D7D744689F57F4FB69AF47409291BEC@SCACMX008.exchad.jpmchase.net> In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47409291BEC@SCACMX008.exchad.jpmchase.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1332964201 news.xs4all.nl 6880 [2001:888:2000:d::a6]:49373 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:22311 On 28/03/2012 20:02, Prasad, Ramit wrote: >> >The right way to convert bytes to strings, and vice versa, is via >> >encoding and decoding operations. >> >> If you want to dictate to the original poster the correct way to do >> things then you don't need to do anything more that. You don't need to >> pretend like Chris Angelico that there's isn't a direct mapping from >> the his Python 3 implementation's internal respresentation of strings >> to bytes in order to label what he's asking for as being "silly". > > It might be technically possible to recreate internal implementation, > or get the byte data. That does not mean it will make any sense or > be understood in a meaningful manner. I think Ian summarized it > very well: > >>You can't generally just "deal with the ascii portions" without >>knowing something about the encoding. Say you encounter a byte >>greater than 127. Is it a single non-ASCII character, or is it the >>leading byte of a multi-byte character? If the next character is less >>than 127, is it an ASCII character, or a continuation of the previous >>character? For UTF-8 you could safely assume ASCII, but without >>knowing the encoding, there is no way to be sure. If you just assume >>it's ASCII and manipulate it as such, you could be messing up >>non-ASCII characters. > > Technically, ASCII goes up to 256 but they are not A-z letters. > Technically, ASCII is 7-bit, so it goes up to 127.