I have the following code
import os
unk2 = os.urandom(10)
totlen = len("lol1") + len("lol2")
ownershipsectionwithsig = "random1" + "random2"
ticket = list(unk2) + list(ownershipsectionwithsig)
ticket = "".join(map(str, ticket))
print(ticket)
the code is for something i was testing related to RNG.
In python 2 it prints the following ?zoռv3L??random1random2
However in python 3 it prints 245148103178837822864207104random1random2
For some reason python 3 doesn't show the raw byte output like python 2 does, it converts it into something. How would i make change my code so python 3 outputs the code the way python 2 does it?
Thanks in advance.
In Python 2 bytes are a sequence of string characters, so converting it into a list by calling list(unk2) turns it into a list of single-character strings:
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> unk2 = os.urandom(10)
>>> list(unk2)
['[', '\x12', '\xfa', '\x98', '\x87', '\x1e', '\n', '\xd1', '\xe2', '\x17']
In Python 3 bytes are a sequence of 8-bit integers, so converting it into a list by calling list(unk2) turns it into a list of integers, and by mapping each integer into a string and joining them together you get the long sequence of digits in the end:
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> unk2 = os.urandom(10)
>>> list(unk2)
[65, 77, 240, 11, 233, 106, 204, 69, 171, 214]
If you want to make Python 3 output the random byte sequence as a string like you do in Python 2 you can convert bytes into a string with the bytes.decode method instead:
unk2 = os.urandom(10).decode('latin-1')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With