I did a lot of research and can't seem to find the answer for this, e.g:
So I have this base64 encoded string: BzM=
, and I know that the correct value is 18.43
This value comes from a decimal field from a MySql database with the following associated definition {'scale': '2', 'decimal.precision': '8'}
, so it's DECIMAL(8,2)
.
The implementation in Java:
String encoded = "BzM=";
int scale = 2; // comes from definition above associated with the field
final BigDecimal decoded = new BigDecimal(new BigInteger(Base64.getDecoder().decode(encoded)), scale);
System.out.println("RES: " + decoded); // RES: 18.43
I'm almost sure that there's an 'easy' way for doing this with Python as well, I searched a lot and couldn't find anything helpful (I may have searched the wrong way).
I tried to reverse engineer this, with no success (can't find where the scale arg enters also):
import base64, struct
x = base64.encodebytes(struct.pack('!f', 18.43))
print(x) # b'QZNwpA==\n' SHOULD BE BzM=
So, I need to simply translate this Java implementation into Python3.