Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 byte string in python2
#1
i want to test something in python2.  to do the test i need a byte string.  i don't need a byte string type.  i only need some way to get a byte string value.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#2
you can iterate through the string, and run  each character through ord():
example:
k = 'kryptonite'
' '.join(format(ord(letter), 'b') for letter in k)
result:
Output:
'1101011 1110010 1111001 1110000 1110100 1101111 1101110 1101001 1110100 1100101'
or for hex:
k = 'kryptonite'
' '.join(format(ord(letter), 'x') for letter in k)
Output:
'6b 72 79 70 74 6f 6e 69 74 65'
Quote
#3
i mean byte string as in b'foo' where for that value, repr()[0] would == 'b'.  but just simply b'foo' doesn't do it.  i have see b' stuff come out sometimes in python2 but that was before i switched to doing things in python3 which makes a proper type for this stuff.  i'm want to see how some code i wrote behaves when given such a value while running under python2.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#4
bytes objects really only exist in Python 3.x.
bytes added in Python 2.6 is an alias to the str type,
that only exists to help writing portable code between Python 2 and 3.

The Doc for this 2.6:
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.
n 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).
his will help the 2to3 converter, which can’t tell whether 2.x code intends strings to contain either characters or 8-bit bytes;
ou 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
ok, so the conditions i wanted to test will not actually happen.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 7 364 Sep-12-2019, 11:19 PM
Last Post: newbieAuggie2019
  Byte string catenation inefficient in 3.7? RMJFlack 13 515 Aug-18-2019, 05:19 AM
Last Post: RMJFlack
  python2.7 executables thus the system python2.7 was erroring utility.execute()? vivekm 1 204 May-20-2019, 11:24 AM
Last Post: vivekm
  HELP: String of Zero's and One's to binary byte schwasskin 1 1,192 May-19-2019, 07:31 AM
Last Post: heiner55
  4 byte hex byte swap from binary file medievil 7 6,793 May-08-2018, 08:16 AM
Last Post: killerrex
  get the content of the byte as string ricardons 5 888 Mar-02-2018, 02:41 PM
Last Post: ricardons
  byte string Skaperen 5 1,087 Feb-04-2018, 08:58 AM
Last Post: Gribouillis
  python2 string formatting - old and new - different for unicode buran 1 1,180 May-16-2017, 10:13 PM
Last Post: Larz60+
  Does Python 3.x have a built-in byte string compare function? Raptor88 2 4,623 Feb-18-2017, 10:44 AM
Last Post: Raptor88

Forum Jump:


Users browsing this thread: 1 Guest(s)