Hello, do you think it is possible to operate with bytes like with ints? useing the fact than ord('A')==65 ; will it possible to use b'A' like 65 or 00100001, in binary, for example for encryption of files, etc... thank you.
On Tue, Feb 11, 2014 at 1:38 PM, Liam Marsh
Hello, do you think it is possible to operate with bytes like with ints? useing the fact than ord('A')==65 ; will it possible to use b'A' like 65 or 00100001, in binary, for example for encryption of files, etc... thank you.
What idea are you proposing here? You can already call ord() on the single byte or iterate a bytes object to get integer values. Being able to treat b"A" itself as identical to the int 65, if that's what you're suggesting, doesn't really make sense.
On Tue, Feb 11, 2014 at 02:21:30PM -0500, Geoffrey Spear wrote:
On Tue, Feb 11, 2014 at 1:38 PM, Liam Marsh
wrote: Hello, do you think it is possible to operate with bytes like with ints? useing the fact than ord('A')==65 ; will it possible to use b'A' like 65 or 00100001, in binary, for example for encryption of files, etc... thank you.
What idea are you proposing here? You can already call ord() on the single byte or iterate a bytes object to get integer values. Being able to treat b"A" itself as identical to the int 65, if that's what you're suggesting, doesn't really make sense.
It makes sense, that it to say the idea isn't entirely incoherent. It just is a bad idea. That is to say, for this idea to work, we'd want things like these to be true: b'A' == 65 which implies that int(b'A') == 65 b'A' < 100 b'CAT' == 4407636 Seems okay so far, but now we get into behaviour that contradicts existing behaviour: b'<'*2 == b'x' # instead of b'<<' b'A' + b'B' == b'\x83' # instead of b'AB' And things which simply look wrong and bizarre: int(b'2') == 50 # instead of 2 b'B' - b'A' == b'\x01' b'A' - b'B' == -1 b'B'//2 == b'!' b'B'/2 == 33.0 It would also imply some pretty strange things about ints: since b'CAT' equals 4407636, and b'CAT'[2] equals 84, does that mean we want to be able to say 4407636[2] and get 84? -- Steven
On 11Feb2014 19:38, Liam Marsh
do you think it is possible to operate with bytes like with ints? useing the fact than ord('A')==65 ; will it possible to use b'A' like 65 or 00100001, in binary, for example for encryption of files, etc...
In Python 2, b'A' makes a str. In python 3, b'A' makes a bytes object, and array of small (8-bit) ints. Element 0 of that array _is_ the byte 65, an integer. So: Python 3.3.3 (default, Nov 23 2013, 13:21:35) [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin Type "help", "copyright", "credits" or "license" for more information.
x=b'A' x b'A' x[0] 65
Just move to python 3 for your code and you'll be better off for what you
seem to be wanting.
Cheers,
--
Cameron Simpson
participants (4)
-
Cameron Simpson
-
Geoffrey Spear
-
Liam Marsh
-
Steven D'Aprano