C# 如何将int转换为uint
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13290603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert int to uint
提问by javapowered
I have intvariable with value 820924when I'm trying to convert it like that:
当我尝试像这样转换它时,我有一个int带值的变量820924:
(uint)data[structure["MICROSECONDS"].Index]
(uint)data[structure["MICROSECONDS"].Index]
it doesn't work.
它不起作用。
This doesn't work as well
这也行不通
unchecked((uint)data[structure["MICROSECONDS"].Index])
unchecked((uint)data[structure["MICROSECONDS"].Index])
I receive Specified cast is not valid.exception.
我收到Specified cast is not valid.异常。
Data stores object, but at run time I should try to convert to int. I'm almost sure. I've printed object value it was 820924, however I don't know how to print object type, but it must be int.
数据存储object,但在运行时我应该尝试转换为int. 我几乎可以肯定。我已经打印了对象值820924,但是我不知道如何打印对象类型,但它必须是 int。
Code:
代码:
object value = data[structure["MICROSECONDS"].Index];
Console.WriteLine("xx MICROSECONDS type " + value.GetType());
Console.WriteLine("xx casting " + value);
Console.WriteLine("xx cast ok" + (uint)value);
Result:
结果:
xx MICROSECONDS type System.Int32
xx casting 820924
采纳答案by Snowbear
First of all you should check the type of your value. You can do it by calling obj.GetType()method (either in your code directly or in Immediate window).
首先,您应该检查您的值的类型。您可以通过调用obj.GetType()方法(直接在您的代码中或在立即窗口中)来完成。
If it is intthen you can do:
如果是,int那么你可以这样做:
uint u = (uint) (int) obj;
Please note that it differs from your cast because it caststo intand then convertsto uintwhile you were trying to castto uint. intcannot be cast to uintand that is why you get the InvalidCastException. intcan be only convertedto uint. It is confusing that both conversionand castoperators look same in code: u = (uint) x.
请注意,您的投不同,因为它投射到int,然后转换到uint时你想投给uint。int不能被强制转换uint,这就是为什么你得到InvalidCastException. int只能转换为uint. 这是混淆了这两个转换和投运营商代码看起来相同:u = (uint) x。
Easier thing you can do is calling a specific method from Convertclass:
您可以做的更简单的事情是从Convert类中调用特定方法:
uint u = Convert.ToUInt32(x);
回答by Dave New
It's possible that the Indexproperty is returning a String or something. You could do the following:
该Index属性可能正在返回字符串或其他内容。您可以执行以下操作:
var num = Convert.ToUInt32(data[structure["MICROSECONDS"].Index]);
Convert.ToUInt32is overloaded with all the relevant types that a uintcan be converted from.
Convert.ToUInt32重载了 auint可以从中转换的所有相关类型。
回答by user1793607
If Index is a string, or has a numeric like representation when converted to a string you can try:
如果 Index 是字符串,或者在转换为字符串时具有类似数字的表示形式,您可以尝试:
UInt32 microSecondsIndex;
if(Uint32.TryParse(data[structure["MICROSECONDS"].Index.ToString()],out microSecondsIndex))
{
//Do Stuff
}
else
{
//Do error handling
}
回答by Nikola Davidovic
The problem is that intis stored as object. Intderives from object but uintdoesn't derive from intso you can't cast intstored as objectto uint. First you have to cast it to intand then to uintbecause that cast is valid. Try it yourself:
问题是int存储为object. Int派生自 object 但uint不派生 fromint因此您不能将int存储转换为objectto uint。首先你必须将它转换为int然后 touint因为该转换是有效的。自己试试:
object o = 5;//this is constant that represents int, constant for uint would be 5u
uint i = (uint)o;//throws exception
But this works:
但这有效:
object o = 5;
int i = (int)o;
uint j = (uint)i;
or
或者
object o = 5;
uint i = (uint)(int)o; //No matter how this looks awkward

