Will Will - 1 month ago 9
C# Question

Is it possible to convert an integer ( short, int, long ) into a base 64 string ( and vice-versa )?

For example

long.TryParse( someString, NumberStyles.HexNumber, new CultureInfo( "en-US" ), out someLong );


will convert a hex string ( 0-9, A-F ) into it's corresponding value.

I want to convert a base 64 value ( 0-9 ( 10 ), a-z ( 26 ), A-Z ( 26 ) ) into it's corresponding integer value.

Is this possible, out of the box?

Answer

Here is how you can round-trip a long to/from Base-64:

long k = 123456789101112L;
string str = Convert.ToBase64String(BitConverter.GetBytes(k));
Console.WriteLine(str);
long x = BitConverter.ToInt64(Convert.FromBase64String(str), 0);
Console.WriteLine("{0} {1} {2}", k, x, x==k);

Demo.

Comments