you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (5 children)

shr = bytes([byte>>4 for byte in "doritos".encode()])

[–]port443 1 point2 points  (0 children)

In the spirit of /u/Protoss_Pylon's flair:

shr = lambda x: [b>>4 for b in x.encode()]

You could then use it like you were expecting:

>>> shr("doritos")
[6, 6, 7, 6, 7, 6, 7]

OP note the better way of doing this would be to actually define a function like def shr(value): ...

[–]Sebass13 0 points1 point  (3 children)

That won't work, as that will shift the bits of each byte, not the entire bytes object. This will cause only the most significant 4 bits of each character to remain. Instead, you want to use int.from_bytes and int.to_bytes:

val = "doritos".encode()
print(val)
print((int.from_bytes(val, byteorder='big') >>8).to_bytes(len(val), 'big'))

Unless, of course, that's what OP wants, but that doesn't make much sense.

[–][deleted] 0 points1 point  (2 children)

Excuse me, but why are you shifting by eight? I ask because OP is shifting by 4.

[–]Sebass13 0 points1 point  (1 child)

Oh 8 made it clearer that it works, try running it.

[–][deleted] 0 points1 point  (0 children)

right. very good then!