C#解析字符串INT32 - 值过大 [英] C# parse string to int32 - value too big

查看:338
本文介绍了C#解析字符串INT32 - 值过大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个字符串从持有 INT32 值的外部接口。该值表示-100 - 一个符号int - 因此,看起来像这串4294967196。
如果它看起来像-100我可以使用 Int32.TryParse()将其转换为符号值。
但在我的情况下,它是作为解释的价值观和告诉我,该值过大(> 2.147.483.647)。
的任何解决办法得到这个工作?如何告诉解析器领先1不是一个数字

I receive a string from an external interface which holds an INT32 value. This value represents "-100" - a signed int - and thus, looking like this string "4294967196". If it would look like "-100" I could use Int32.TryParse() to cast it to a signed value. But in my case it interprets the values as is and tells me that the value is too big (>2.147.483.647). Any workaround to get this working? How to tell the parser that the leading 1 is not a number?

编辑:对不起,不准确。我收到的值是一个字符串,它看起来像这样4294967196。它代表一个 UINT32 的值-100。如果该接口将返回一个字符串抱着-100将有可能只使用 Int32.TryParse()。这就是我想表达的。

Sorry for being inaccurate. The value I receive is a string that looks like this "4294967196". It represents an Uint32 with the value -100. If the interface would return a string holding "-100" it would be possible to just use Int32.TryParse(). That's what I was trying to express.

推荐答案

使用 uint.TryParse()和铸造结果为 INT

string s = "4294967196";
uint ux;
int x = 0;
if (uint.TryParse(s, out ux))
{
    x = (int)ux;
}
// x = -100

这篇关于C#解析字符串INT32 - 值过大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆