Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 get the content of the byte as string
#1
hi

i want to transform

b'p107\xff\xff\xff'
i.e a byte

into

'p107\xff\xff\xff'
i.e a string


how is it done
Quote
#2
str(b'p107\xff\xff\xff')
Quote
#3
To convert byte strings into string use the decode() method (assuming the byte strings represent unicode strings encoded in some encoding)
>>> bx = b'p107\xff\xff\xff'
>>> sx = bx.decode('latin-1')
>>> sx
'p107ÿÿÿ'
Quote
#4
thanks the str() was what i was looking for
Quote
#5
if i have

a='x02'

how can i make

b='\x02'

if i do
b="\"+ a
i have an error SyntaxError: EOL while scanning string literal

if i do
b="\\"+a
print(b)
b='\\x02'

which has \\ instead of \

if i do

ord('\x02') results in 2

str='\\'+'x02'
ord(str) gives error
Quote
#6
if i have

a='x02'

how can i make

b='\x02'

if i do
b="\"+ a
i have an error SyntaxError: EOL while scanning string literal

if i do
b="\\"+a
print(b)
b='\\x02'

which has \\ insted of \
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
  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
  byte string Skaperen 5 1,087 Feb-04-2018, 08:58 AM
Last Post: Gribouillis
  byte string in python2 Skaperen 4 1,504 Nov-23-2017, 03:13 AM
Last Post: Skaperen
  Does Python 3.x have a built-in byte string compare function? Raptor88 2 4,621 Feb-18-2017, 10:44 AM
Last Post: Raptor88

Forum Jump:


Users browsing this thread: 1 Guest(s)