我想将C#.NET存储ulong
到T-SQL数据库中.我没有看到任何相关的规定,因为SQL bigint
具有与正常相同的最小/最大值long
.
有什么方法可以做到这一点吗?还是抓住了OverflowException
我唯一的希望?
我正在构建一个将由C++使用COM的DLL.请让我知道C++ 64位无符号long long的C#等价物.
它会是C#中的ulong类型吗?请确认.
谢谢,Gagan
如果我尝试使用BitConverter,它需要一个字节数组,我没有.我有一个Int32,我想将其转换为UInt32.
在C++中没有问题.
在c#中将两个uint组合成ulong的最佳方法是什么,设置高/低uints.
我知道bitshifting可以做到这一点,但我不知道语法,或者可能有其他API来帮助像BitConverter,但我没有看到一个方法做我想要的.
我有什么理由不能使用以下代码吗?
ulong test(int a, int b)
{
return a == b ? 0 : 1;
}
Run Code Online (Sandbox Code Playgroud)
它告诉我:
Cannot implicitly convert type 'int' to 'ulong'. An explicit conversion exists (are you missing a cast?)
Run Code Online (Sandbox Code Playgroud)
以下将有效:
ulong test(int a, int b)
{
return false ? 0 : 1;
}
Run Code Online (Sandbox Code Playgroud)
我知道如何解决这个问题.我只是想知道原因.
谢谢.
我试图以一种非常简单的方式计算C#中的Fibonacci序列,但是当涉及到更高的数字时,它会通过给出错误的答案来阻止并停止工作.
ulong num = 1;
ulong lnum = 0;
uint x = 1;
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("(0) " + 1);
}
private void timer1_Tick(object sender, EventArgs e)
{
if (x <= 1000)
{
ulong newnum = lnum + num;
listBox1.Items.Add("(" + x + ") " + newnum);
listBox1.SetSelected((int)x, true);
lnum = num;
num = newnum;
x++;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在通过一种方式制作它,我可以通过一次将它们添加到列表框1来添加数字.
我试图将ulong映射到long(反之亦然),并将uint映射到int(反之亦然),如下所示 - 以便将值保存在带有签名类型的MS-SQL数据库中仅限整数和大整数.
我这样做是因为我必须检查(在数据库中)一个数字(uint,ulong)是否在一堆uint/ulong范围内的哪个范围内(IPs-v4&v6;实际上ulong实际上是由uint128组成的两个ulongs).
有没有更有效的方法来实现这一点,我有这里的代码:
public static ulong SignedLongToUnsignedLong(long signedLongValue)
{
ulong backConverted = 0;
// map ulong to long [ 9223372036854775808 = abs(long.MinValue) ]
if (signedLongValue < 0)
{
// Cannot take abs from MinValue
backConverted = (ulong)System.Math.Abs(signedLongValue - 1);
backConverted = 9223372036854775808 - backConverted - 1;
}
else
{
backConverted = (ulong)signedLongValue;
backConverted += 9223372036854775808;
}
return backConverted;
}
public static long UnsignedLongToSignedLong(ulong unsignedLongValue)
{
// map ulong to long [ 9223372036854775808 = abs(long.MinValue) ]
return (long) (unsignedLongValue …
Run Code Online (Sandbox Code Playgroud) 我对ulong
类型的行为有点困惑。我理解它是一个用于正数的64 位整数(最大值 18,446,744,073,709,551,615)。我刚开始使用它是因为我需要非常大的正数,但是在潜在的负数周围有一些我不明白的奇怪行为。
ulong big = 1000000; //Perfectly legal
ulong negative = -1; //"Constant value "-1" cannot be converted to a ulong" Makes sense
//Attempt to sneak around the -1 as a constant compile-time error
int negativeInt = -1;
ulong negativeUlongFail = negativeInt; //"Cannot implicitly convert 'int' to 'ulong'.
//An explicit conversion exists (are you missing a cast?)"
//So I add casting
ulong negativeUlong = (ulong)negativeInt; //But this yields 18446744073709551615
//Try and get a …
Run Code Online (Sandbox Code Playgroud) 在 C# 中,有一个System.Threading.Tasks.Parallel.For(...)
which 的作用与循环相同for
,没有顺序,但在多个线程中。问题是,它只适用于long
和int
,我想使用ulong
。好的,我可以打字,但我在边框方面遇到了一些问题。
比方说,我想要一个从long.MaxValue-10
到 的循环long.MaxValue+10
(记住,我说的是ulong
)。我怎么做?
一个例子:
for (long i = long.MaxValue - 10; i < long.MaxValue; ++i)
{
Console.WriteLine(i);
}
//does the same as
System.Threading.Tasks.Parallel.For(long.MaxValue - 10, long.MaxValue, delegate(long i)
{
Console.WriteLine(i);
});
//except for the order, but theres no equivalent for
long max = long.MaxValue;
for (ulong i = (ulong)max - 10; i < (ulong)max + 10; ++i)
{
Console.WriteLine(i);
}
Run Code Online (Sandbox Code Playgroud) 如何将ulong转换为long,结果与我在c ++中得到的结果相同.
C++
unsigned long ul = 3633091313;
long l = (long)ul;
l is -661875983
Run Code Online (Sandbox Code Playgroud)
C#
ulong ul = 3633091313;
long l = (long)ul;
l is 3633091313
Run Code Online (Sandbox Code Playgroud)