views:

94

answers:

1

I'm trying to convert a 2.5 program to 3.

Is there a way in python 3 to change a byte string, such as b'\x01\x02' to a python 2.5 style string, such as '\x01\x02', so that string and byte-by-byte comparisons work similarly to 2.5? I'm reading the string from a binary file.

I have a 2.5 program that reads bytes from a file, then compares or processes each byte or combination of bytes with specified constants. To run the program under 3, I'd like to avoid changing all my constants to bytes and byte strings ('\x01' to b'\x01'), then dealing with issues in 3 such as:

a = b'\x01'
b = b'\x02'

results in

(a+b)[0] != a

even though similar operation work in 2.5. I have to do (a+b)[0] == ord(a), while a+b == b'\x01\x02' works fine. (By the way, what do I do to (a+b)[0] so it equals a?)

Unpacking structures is also an issue.

Am I missing something simple?

+3  A: 

Bytes is an immutable sequence of integers (in the range 0<= to <256), therefore when you're accessing (a+b)[0] you're getting back an integer, exactly the same one you'd get by accessing a[0]. so when you're comparing sequence a to an integer (a+b)[0], they're naturally different.

using the slice notation you could however get a sequence back:

>>> (a+b)[:1] == a         # 1 == len(a) ;)
True

because slicing returns bytes object.

I would also advised to run 2to3 utility (it needs to be run with py2k) to convert some code automatically. It won't solve all your problems, but it'll help a lot.

SilentGhost
>>so when you're comparing sequence a to an integer (a+b)[0], they're naturally different.>> Except that was not the case for analogous items in 2.5. I realize why this makes sense in the context of py3k, but it makes conversion tedious
foosion
2to3 (run under py3k) was helpful for adding parens to print statements. It did nothing for the bytes v. string issues, for which it would be nice to find a general solution.
foosion