C#不支持ulong到bin / oct / dec / hex字符串 - > Convert.ToString(ulong,base)

时间:2015-12-09 23:52:09

标签: c# string bin octal ulong

我将ulong值的bin / oct / dec / hex值作为字符串。所以我必须使用convert.tostring(,base)和所需的base。为了支持这一点,我将ulong值转换为long,而convert.tostring(,base)支持long以将bin / oct / dec / hex值作为ulong的字符串。我对吗?

//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

好的,对于dec,我只能使用ul.ToString(); for Hex,ul.ToString(" X");

基于,Copy bits from ulong to long in C#我有点困惑。这是否为ulong提供了正确的Oct和Bin字符串表示?

相关要点: https://gist.github.com/chubbson/375b535243c166d28119

2 个答案:

答案 0 :(得分:0)

只要您要转换为的类型没有负面概念,您的转换过程就应该有效,因此除了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

答案 1 :(得分:0)

Convert.ToString只允许对byte,int,short和long类型进行基本转换。 以下BasedValueConverter允许bin / oct / dec / hex表示byte,sbyte,short,ushort,int,uint,long,ulong。

检查相关https://gist.github.com/chubbson/375b535243c166d28119的BasedValueConverter类实现。 这是输出(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