I want to convert a BigInteger to a text string in the optimal way. There are 95 printable ASCII characters, numbered 32 to 126. I want to use these characters to convert BigInteger to text. Same as this code, but is for Uint:
static string ConvertToBase64Arithmetic(uint i)
{
const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
StringBuilder sb = new StringBuilder();
do
{
sb.Insert(0, alphabet[(int)(i % 64)]);
i = i / 64;
} while (i != 0);
return sb.ToString();
}
My goal is to make a smaller text. It is clear that if it is done as byte storage( BigInteger.ToByteArray()), it is the most optimal mode. But I'm just looking for a shorter text string lenght and Base95 is just a suggestion. my code:
static string ConvertToBase95Arithmetic(BigInteger i)
{
const string alphabet = "mcW=2`R\\.5+46L\" !#$%&'()*,-/013789:;<=?@ABCDEFGHIJKMNOPQSTUVXYZ[]^_abdefghijklnopqrstuvwxyz{|}~";
StringBuilder sb = new StringBuilder();
do
{
sb.Insert(0, alphabet[(BigInteger)(i % 95)]);
i = i / 64;
} while (i != 0);
return sb.ToString();
}
It is natural that this text string can be converted back to the original number and the data will not be lost.