强制转换溢出如何处理
当Long型数值转换到Integer类型时,如何才能不提示,而是直接舍弃溢出的部分,有没有类似于C#语言里的Unchecked语句?
[解决办法]
“c#语言里的Unchecked语句”说明程序设计者就应该这样使用long型变量吗?那么c#语言程序员都应该改用c语言而放弃c#吧?
你完全没有必要纠结于这种“unchecked”。如果出现了这种纠结,那么你连c#其实都不应该使用了。
[解决办法]
vb.net没有unchecked这类的关键字。
你可以为整个工程设置:
To set /removeintchecks in the Visual Studio integrated development environment
1. Have a project selected in Solution Explorer. On the Project menu, click Properties. For more information, see Introduction to the Project Designer.
2. Click the Compile tab.
3. Click the Advanced button.
4. Modify the value of the Remove integer overflow checks box.
参考:
http://msdn.microsoft.com/en-us/library/key2x70f(v=vs.90).aspx
[解决办法]
(1)
i = BitConverter.GetInt32(BitConverter.GetBytes(l), 0)
(2)
i = l Mod (2 ^ 32)