Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!news.dfncis.de!not-for-mail From: Peter Daum Newsgroups: comp.lang.python Subject: Re: "convert" string to bytes without changing data (encoding) Date: Wed, 28 Mar 2012 11:43:52 +0200 Lines: 26 Message-ID: <9tg4qoFbfpU1@mid.dfncis.de> References: <9tg21lFmo3U1@mid.dfncis.de> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: news.dfncis.de MWUjEaXgtm7K89Ewo5fd9g5tkJwQFG8qJ42xr7JIDa5NbBfGO869Folsgo Cancel-Lock: sha1:qvTHaZEEfzP/QU7kmqgHAd+G3mg= User-Agent: Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.9.1.16) Gecko/20101125 Thunderbird/3.0.11 In-Reply-To: Xref: csiph.com comp.lang.python:22270 On 2012-03-28 11:02, Chris Angelico wrote: > On Wed, Mar 28, 2012 at 7:56 PM, Peter Daum wrote: >> is there any way to convert a string to bytes without >> interpreting the data in any way? Something like: >> >> s='abcde' >> b=bytes(s, "unchanged") > > What is a string? It's not a series of bytes. You can't convert it > without encoding those characters into bytes in some way. ... in my example, the variable s points to a "string", i.e. a series of bytes, (0x61,0x62 ...) interpreted as ascii/unicode characters. b=bytes(s,'ascii') # or ('utf-8', 'latin1', ...) would of course work in this case, but in general, if s holds any data with bytes > 127, the actual data will be changed according to the provided encoding. What I am looking for is a general way to just copy the raw data from a "string" object to a "byte" object without any attempt to "decode" or "encode" anything ... Regards, Peter