13

In python 3.2, i can change the type of an object easily. For example :

x=0
print(type (x))
x=bytes(0)
print(type (x))

it will give me this :

<class 'int'>
<class 'bytes'>

But, in python 2.7, it seems that i can't use the same way to do it. If i do the same code, it give me this :

<type 'int'>
<type 'str'>

What can i do to change the type into a bytes type?

1
  • Martin's answer has the relevant info. Commented Nov 20, 2012 at 15:52

5 Answers 5

16

You are not changing types, you are assigning a different value to a variable.

You are also hitting on one of the fundamental differences between python 2.x and 3.x; grossly simplified the 2.x type unicode has replaced the str type, which itself has been renamed to bytes. It happens to work in your code as more recent versions of Python 2 have added bytes as an alias for str to ease writing code that works under both versions.

In other words, your code is working as expected.

Sign up to request clarification or add additional context in comments.

2 Comments

so in 2.7 , is there any ways for me to do the same thing in 3.2? i just want it to be in a bytes type.
Type str in python 2.7 is the bytes type.
8

What can i do to change the type into a bytes type?

You can't, there is no such type as 'bytes' in Python 2.7.

From the Python 2.7 documentation (5.6 Sequence Types): "There are seven sequence types: strings, Unicode strings, lists, tuples, bytearrays, buffers, and xrange objects."

From the Python 3.2 documentation (5.6 Sequence Types): "There are six sequence types: strings, byte sequences (bytes objects), byte arrays (bytearray objects), lists, tuples, and range objects."

1 Comment

>>> str is bytes ... True -- there is a binary sequence type in python2.x -- it's str!
4

In Python 2.x, bytes is just an alias for str, so everything works as expected. Moreover, you are not changing the type of any objects here – you are merely rebinding the name x to a different object.

1 Comment

so in 2.7 , is there any ways for me to do the same thing in 3.2? i just want it to be in a bytes type.
1

May be not exactly what you need, but when I needed to get the decimal value of the byte d8 (it was a byte giving an offset in a file) i did:

a = (data[-1:])          # the variable 'data' holds 60 bytes from a PE file, I needed the last byte
                         #so now a == '\xd8'  , a string
b = str(a.encode('hex')) # which makes b == 'd8' , again a string
c = '0x' + b             # c == '0xd8' , again a string
int_value = int(c,16)    # giving me my desired offset in decimal: 216

                         #I hope this can help someone stuck in my situation

Comments

0

Just example to emphasize a procedure of turning regular string into binary string and back:

sb = "a0" # just string with 2 characters representing a byte
ib = int(sb, 16) # integer value (160 decimal)
xsb = chr(ib) # a binary string (equals '\xa0')

Now backwards

back_sb = xsb.encode('hex')
back_sb == sb # returns True

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.