C#解析字符串INT32

编程入门 行业动态 更新时间:2024-10-26 18:25:41
本文介绍了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

本文发布于:2023-11-06 04:11:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562682.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!