3

I would have the bin/oct/dec/hex values of ulong values as string. So I have to use convert.tostring(, base) with desired base. To support this, I cast the ulong value to long, while long is supported with convert.tostring(, base) to have the bin/oct/dec/hex value as string for ulong. Am I right?

//while Convert.ToString does not support ulong with base
//Convert.ToString(ulong.MaxValue, 2);

// following code sample is the same like not supported ToString on line 2, right? 
ulong ul = ulong.Maxvalue; 
long l = (long)ul;
Convert.ToString(l, 8); //8 => oct, 2 => bin

OK, for dec, I just can use ul.ToString(); for Hex, ul.ToString("X");

Based on, Copy bits from ulong to long in C# I'm a little confused. Does this give me the correct Oct and Bin string representation for ulong?

related gist: https://gist.github.com/chubbson/375b535243c166d28119

Community
  • 1
  • 1
had
  • 1,828
  • 1
  • 12
  • 9

2 Answers2

1

Your conversion process should work as long as the types you are converting to does not have the concept of negative, so you should be safe to do every base except 10.

public static void Main()
{
    ulong ul = ulong.MaxValue; 
    long l = (long)ul;
    var s = Convert.ToString(l, 8); //8 => oct, 2 => bin
    Console.WriteLine(s); //Outputs 1777777777777777777777
}

https://dotnetfiddle.net/fsoIkw

Scott Chamberlain
  • 116,967
  • 28
  • 260
  • 389
0

Convert.ToString just allows base convert for types byte, int, short and long. Following BasedValueConverter allows bin/oct/dec/hex representation for byte, sbyte, short, ushort, int, uint, long, ulong.

check related https://gist.github.com/chubbson/375b535243c166d28119 for the BasedValueConverter class implementation. This is the output (LinqpadScript):

BasedValueConverter bvc;
bvc = new BasedValueConverter(byte.MaxValue.Dump("BasedValueConverter byte"));
bvc.ToStringBin().Dump();  // 11111111
bvc.ToStringOct().Dump();  // 377
bvc.ToStringDec().Dump();  // 255
bvc.ToStringHex().Dump();  // FF

bvc = new BasedValueConverter(byte.MinValue);
bvc.ToStringBin().Dump();  // 00000000
bvc.ToStringOct().Dump();  // 000
bvc.ToStringDec().Dump();  // 000
bvc.ToStringHex().Dump();  // 00



bvc = new BasedValueConverter(sbyte.MaxValue.Dump("BasedValueConverter sbyte"));
bvc.ToStringBin().Dump();  // 01111111
bvc.ToStringOct().Dump();  // 177
bvc.ToStringDec().Dump();  // 127
bvc.ToStringHex().Dump();  // 7F

bvc = new BasedValueConverter((sbyte)-1);
bvc.ToStringBin().Dump();  // 11111111
bvc.ToStringOct().Dump();  // 377
bvc.ToStringDec().Dump();  // -001
bvc.ToStringHex().Dump();  // FF



bvc = new BasedValueConverter(short.MaxValue.Dump("BasedValueConverter short"));
bvc.ToStringBin().Dump();  // 0111111111111111
bvc.ToStringOct().Dump();  // 077777 
bvc.ToStringDec().Dump();  // 32767
bvc.ToStringHex().Dump();  // 7FFF

bvc = new BasedValueConverter((short)-1);
bvc.ToStringBin().Dump();  // 1111111111111111
bvc.ToStringOct().Dump();  // 177777
bvc.ToStringDec().Dump();  // -00001
bvc.ToStringHex().Dump();  // FFFF



bvc = new BasedValueConverter(ushort.MaxValue.Dump("BasedValueConverter ushort"));
bvc.ToStringBin().Dump();  // 1111111111111111
bvc.ToStringOct().Dump();  // 177777
bvc.ToStringDec().Dump();  // 65535
bvc.ToStringHex().Dump();  // FFFF

bvc = new BasedValueConverter(ushort.MinValue);
bvc.ToStringBin().Dump();  // 0000000000000000
bvc.ToStringOct().Dump();  // 000000
bvc.ToStringDec().Dump();  // 00000
bvc.ToStringHex().Dump();  // 0000



bvc = new BasedValueConverter(int.MaxValue.Dump("BasedValueConverter int"));
bvc.ToStringBin().Dump();  // 01111111111111111111111111111111
bvc.ToStringOct().Dump();  // 17777777777
bvc.ToStringDec().Dump();  // 2147483647
bvc.ToStringHex().Dump();  // 7FFFFFFF

bvc = new BasedValueConverter((int)-1);
bvc.ToStringBin().Dump();  // 11111111111111111111111111111111
bvc.ToStringOct().Dump();  // 37777777777
bvc.ToStringDec().Dump();  // -0000000001
bvc.ToStringHex().Dump();  // FFFFFFFF



bvc = new BasedValueConverter(uint.MaxValue.Dump("BasedValueConverter uint"));
bvc.ToStringBin().Dump();  // 11111111111111111111111111111111
bvc.ToStringOct().Dump();  // 37777777777
bvc.ToStringDec().Dump();  // 4294967295
bvc.ToStringHex().Dump();  // FFFFFFFF

bvc = new BasedValueConverter(uint.MinValue);
bvc.ToStringBin().Dump();  // 00000000000000000000000000000000
bvc.ToStringOct().Dump();  // 00000000000
bvc.ToStringDec().Dump();  // 0000000000
bvc.ToStringHex().Dump();  // 00000000



bvc = new BasedValueConverter(long.MaxValue.Dump("BasedValueConverter long"));
bvc.ToStringBin().Dump();  // 0111111111111111111111111111111111111111111111111111111111111111
bvc.ToStringOct().Dump();  // 0777777777777777777777
bvc.ToStringDec().Dump();  // 9223372036854775807
bvc.ToStringHex().Dump();  // 7FFFFFFFFFFFFFFF

bvc = new BasedValueConverter((long)-1);
bvc.ToStringBin().Dump();  // 1111111111111111111111111111111111111111111111111111111111111111
bvc.ToStringOct().Dump();  // 1777777777777777777777
bvc.ToStringDec().Dump();  // -00000000000000000001
bvc.ToStringHex().Dump();  // FFFFFFFFFFFFFFFF



bvc = new BasedValueConverter(ulong.MaxValue.Dump("BasedValueConverter ulong"));
bvc.ToStringBin().Dump();  // 1111111111111111111111111111111111111111111111111111111111111111
bvc.ToStringOct().Dump();  // 1777777777777777777777
bvc.ToStringDec().Dump();  // -00000000000000000001
bvc.ToStringHex().Dump();  // FFFFFFFFFFFFFFFF

bvc = new BasedValueConverter(ulong.MinValue);
bvc.ToStringBin().Dump();  // 0000000000000000000000000000000000000000000000000000000000000000
bvc.ToStringOct().Dump();  // 0000000000000000000000
bvc.ToStringDec().Dump();  // 00000000000000000000
bvc.ToStringHex().Dump();  // 0000000000000000
had
  • 1,828
  • 1
  • 12
  • 9