Convert Base64 encoded bytes to Int - python

So basically, I generate 16 random bytes and i then convert them to Base64. I need to transform this Base64 to an Int.
I've searched all over the internet, i found out how to convert to hex, and many other but none seem to work.
This is the code I use to generate the nonce :
import base64
nonce = base64.encodebytes(os.urandom(16))
I need a function a bit like the parseInt() in JavaScript. The result need to be between -9223372036854775808 and 9223372036854775807.

There is a builtin method to convert bytes to int:
int.from_bytes(nonce, "big") # big endian
int.from_bytes(nonce, "little") # little endian
Python docs: https://docs.python.org/3/library/stdtypes.html#int.from_bytes

Related

how to convert python binary str literal to real bytes

In python 3, I have a str like this, which is the exactly literal representation of bytes data:
'8\x81p\x925\x00\x003dx\x91P\x00x\x923\x00\x00\x91Pd\x00\x921d\x81p1\x00\x00'
I would like to convert it to real byte,
b'8\x81p\x925\x00\x003dx\x91P\x00x\x923\x00\x00\x91Pd\x00\x921d\x81p1\x00\x00'
I tried to use .encode() on the str data, but the result added many "xc2":
b'8\xc2\x81p\xc2\x925\x00\x003dx\xc2\x91P\x00x\xc2\x923\x00\x00\xc2\x91Pd\x00\xc2\x921d\xc2\x81p1\x00\x00'.
I also tried:
import ast
ast.literal_eval("b'8\x81p\x925\x00\x003dx\x91P\x00x\x923\x00\x00\x91Pd\x00\x921d\x81p1\x00\x00'")
The result is:
ValueError: source code string cannot contain null bytes
How to convert the str input to the bytes as exactly the same as follows?
b'8\x81p\x925\x00\x003dx\x91P\x00x\x923\x00\x00\x91Pd\x00\x921d\x81p1\x00\x00'
You are on the right track with the encode function already. Just try with this encoding:
>>> '8\x81p\x925\x00\x003dx\x91P\x00x\x923\x00\x00\x91Pd\x00\x921d\x81p1\x00\x00'.encode('raw_unicode_escape')
b'8\x81p\x925\x00\x003dx\x91P\x00x\x923\x00\x00\x91Pd\x00\x921d\x81p1\x00\x00'
I took it from this table in Python's codecs documentation
Edit: I just found it needs raw_unicode_escape instead of unicode_escape

Why protobuf/python do base64 encode for bytes field in MessageToDict function

when I use json_format.MessageToDict to convert the protobuf message to python dict. the bytes type field will become the base64 encoding.
I find that source code:
https://chromium.googlesource.com/external/github.com/google/protobuf/+/HEAD/python/google/protobuf/json_format.py#289
But why protobuf do that?
json cannot keep data in bytes. In order to put bytes inside json, you need something to encode the bytes. base64 is a common method for doing that.
As named, json_format.MessageToDict, it converts bytes into base64 encoded string to you.
Similar question here

Python: Converting a string to the octet format

I am trying to implement the OS2IP algorithm in Python. However I do not know how I can convert a character string, say "Men of few words are the best men." into the octet format.
Use the .encode() method of str. For example:
"öä and ü".encode("utf-8")
displays
b'\xc3\xb6\xc3\xa4 and \xc3\xbc'
If you then want to convert this to an int, you can just use the int.from_bytes() method, e.g.
the_bytes = "öä and ü".encode("utf-8")
the_int = int.from_bytes(the_bytes, 'big')
print(the_int)
displays
236603614466389086088250300
In preparing for an RSA encryption, a padding algorithm is typically applied to the result of the first encoding step to pad the byte array out to the size of the RSA modulus, and then padded byte array is converted to an integer. This padding step is critical to the security of RSA cryptography.

Hex to int32 Big Endian

I am trying to convert this hex to the correct INT32 Big Endian that would be:
ffd7c477 --> -2636681
I checked how it should look here:
http://www.scadacore.com/tools/programming-calculators/online-hex-converter/
I dont know how to convert it. This is where the latitude is
payload = "1901000a03010aff01ff01300a01ffd7c4750016c0540322ed"
latitude = payload[28:36] = ffd7c477
Here I get the wrong unsigned value:
int(binary[28:36], 16)
This worked struct.unpack('>i', "ffd7c477".decode('hex'))
Since Python will use the byteorder of your processor architecture by default to handle numbers (you can check your systems byteorder with sys.byteorder), you'll have to explicitly specify that you want to treat the given value as big endian. The struct module will allow you to do this:
import struct, codecs
val = "ffd7c477"
struct.unpack("!i", codecs.decode(val, "hex"))
The first argument of unpack: ! means to treat the bytes as big endian, i means to treat the bytes as int32 values.

python 2.7 - converting float to bytes and looping through the bytes

I'm trying to send a float as a series of 4 bytes over serial.
I have code that looks like this which works:
ser.write(b'\xcd') #sending the byte representation of 0.1
ser.write(b'\xcc')
ser.write(b'\xcc')
ser.write(b'\x3d')
but I want to be able to send an arbitary float.
I also want to be able to go through each byte individually so this won't do for example:
bytes = struct.pack('f',float(0.1))
ser.write(bytes)
because I want to check each byte.
I'm using python 2.7
How can I do this?
You can use the struct module to pack the float as binary data. Then loop through each byte of the bytearray and write them to your output.
import struct
value = 13.37 # arbitrary float
bin = struct.pack('f', value)
for b in bin:
ser.write(b)

Categories