Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 byte string
#1
i can do x=b'foo' then [y for y in x] gives me [102, 111, 111]. how can i reverse that so [98, 97, 114] -> b'bar'?

i found my own answer in Python3: bytes(bytearray([98, 97, 114])) -> b'bar'. there can be no answer in Python2 because b'anything' cannot exist in Python2.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#2
(Feb-02-2018, 03:03 AM)Skaperen Wrote: there can be no answer in Python2 because b'anything' cannot exist in Python2.
In python2, b'foo' exists, and it is equal to 'foo'.
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 'anything'
>>> x = 'anything'
>>> a = bytearray(x)
>>> list(a)
[97, 110, 121, 116, 104, 105, 110, 103]
>>> str(a)
'anything'
>>> L = list(a)
>>> str(bytearray(L))
'anything'
Quote
#3
try doing that without bytearray.

i'd argue that b'foo' (a literal) becomes a regular character string because bytes==str is True. does this work in 2.0? does 2.0 matter any more?

or can you make any code which produces an actual bytes (not bytearray) type?

i've been doing stuff in Python3. but i still like to make my code work (and work right) in Python2. that is because many systems still come with Python2 but not Python3 ready to use (even though installing or upgrading to Python3 is an available option for many).
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#4
(Feb-03-2018, 03:34 AM)Skaperen Wrote: or can you make any code which produces an actual bytes (not bytearray) type?
No the new bytes type is 3.x only.
As Grib show so is the 2.6 --> 2.7 bytes built-in just an alias to the str type.
But bytes in 2.6 --> 2.7is not the same as bytes in Python 3.
The Doc Byte Literals
Quote:Python 2.6 adds bytes as a synonym for the str type, and it also supports the b'' notation.

The 2.6 str differs from 3.0’s bytes type in various ways;
most notably, the constructor is completely different.
In 3.0, bytes([65, 66, 67]) is 3 elements long, containing the bytes representing ABC;
in 2.6, bytes([65, 66, 67]) returns the 12-byte string representing the str() of the list.

The primary use of bytes in 2.6 will be to write tests of object type such as isinstance(x, bytes).
This will help the 2to3 converter, which can’t tell whether 2.x code intends strings to contain either characters or 8-bit bytes;
you can now use either bytes or str to represent your intention exactly, and the resulting code will also be correct in Python 3.0.
Quote
#5
2.x lets you refer to name "bytes" which is the same as name "str". 2.x lets you express literal strings with a "b" prefix which have the same value as the same string without the "b" prefix. does this mean "2.x has bytes" or not? even though if bytes==str: is my quick test to see if my code is running in 2.x before i import sys, i say "not".

but i was basically saying (expressed with more detail this time) "you cannot, in 2.x, create and reference a data type that is an immutable type bytes, but sure you can do so a data type that is a mutable equivalent. the literal b'anything' certainly does not do this in 2.x ".
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#6
Here is what python 2.7 says:
>>> bytes is str
True
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  First Byte of a string is missing while receiving data over TCP Socket shahrukh1987 3 157 Nov-20-2019, 10:34 AM
Last Post: shahrukh1987
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 7 2,184 Sep-12-2019, 11:19 PM
Last Post: newbieAuggie2019
  Byte string catenation inefficient in 3.7? RMJFlack 13 663 Aug-18-2019, 05:19 AM
Last Post: RMJFlack
  HELP: String of Zero's and One's to binary byte schwasskin 1 1,238 May-19-2019, 07:31 AM
Last Post: heiner55
  4 byte hex byte swap from binary file medievil 7 8,017 May-08-2018, 08:16 AM
Last Post: killerrex
  get the content of the byte as string ricardons 5 957 Mar-02-2018, 02:41 PM
Last Post: ricardons
  byte string in python2 Skaperen 4 1,604 Nov-23-2017, 03:13 AM
Last Post: Skaperen
  Does Python 3.x have a built-in byte string compare function? Raptor88 2 5,132 Feb-18-2017, 10:44 AM
Last Post: Raptor88

Forum Jump:


Users browsing this thread: 1 Guest(s)