In Java, I can encode BigInteger as:
java.math.BigInteger bi = new java.math.BigInteger("65537L"); String encoded = Base64.encodeBytes(bi.toByteArray(), Base64.ENCODE|Base64.DONT_GUNZIP); // result: 65537L encodes as "AQAB" in Base64 byte[] decoded = Base64.decode(encoded, Base64.DECODE|Base64.DONT_GUNZIP); java.math.BigInteger back = new java.math.BigInteger(decoded);
In C #:
System.Numerics.BigInteger bi = new System.Numerics.BigInteger("65537L"); string encoded = Convert.ToBase64(bi); byte[] decoded = Convert.FromBase64String(encoded); System.Numerics.BigInteger back = new System.Numerics.BigInteger(decoded);
How can I encode long integers in Python as Base64 encoded strings? What I tried so far gives results different from implementations in other languages โโ(so far I tried in Java and C #), in particular, it creates Base64-encoded strings in length encoded.
import struct encoded = struct.pack('I', (1<<16)+1).encode('base64')[:-1]
When using this Python code to create a Base64 encoded string, the resulting decoded integer in Java (for example) instead of 65537 creates instead of 16777472 . First of all, what am I missing?
Secondly, I need to find out manually which length format to use in struct.pack ; and if I try to encode a long number (greater than (1<<64)-1 ), the 'Q' format specification is too short to preserve the view. Does this mean that I have to do the presentation manually, or is there a documentless format specifier for the struct.pack function? (I am not forced to use a struct , but at first glance it seemed to do what I needed.)
jbatista
source share