gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:40:35 33 4
gpt4 key购买 nike

我会将 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

好的,对于 12 月,我只能使用 ul.ToString();对于十六进制,ul.ToString("X");

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

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

最佳答案

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

关于C# 不支持 ulong 到 bin/oct/dec/hex 字符串 -> Convert.ToString(ulong, base),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34191275/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com