3

我会将 ulong 值的 bin/oct/dec/hex 值作为字符串。所以我必须使用 convert.tostring(, 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(); 对于十六进制,ul.ToString("X");

基于,在 C# 中将位从 ulong 复制到 long我有点困惑。这是否给了我正确的 ulong 的 Oct 和 Bin 字符串表示?

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

4

2 回答 2

1

只要您要转换的类型没有负数的概念,您的转换过程就应该有效,因此您应该安全地执行除 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

于 2015-12-10T00:43:58.823 回答
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
于 2015-12-10T11:28:47.820 回答