vb.net VB - 将长字符串数字转换为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18483968/
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
VB - Convert long String-number to Integer
提问by Aleksander Fimreite
Alright, so I'm not exactly very oriented when it comes to functions aviable in VB. I have a string containing the current date and time and need to convert it to a integer so i can compare the time.
好的,所以当涉及到 VB 中可用的函数时,我并不是很明确。我有一个包含当前日期和时间的字符串,需要将其转换为整数,以便比较时间。
Dim my_str as String = "201308281110"
Dim my_int as Integer = Convert.ToInt32(my_str)
I cant do that with this string apparently. Because I think it is too long for the 32-bit integer. All the other convertions I have tried also fails. That including "ToInt64", "Int", "CInt"... So, any idea how to convert this longer string to an integer?
我显然不能用这个字符串来做到这一点。因为我认为32位整数太长了。我尝试过的所有其他转换也失败了。包括“ToInt64”、“Int”、“CInt”……那么,知道如何将这个较长的字符串转换为整数吗?
回答by sloth
Why don't you just simply use Date? You can compare Dateswith each other, so there's no need to use an integer for comparing.
你为什么不简单地使用Date?您可以相互比较Dates,因此无需使用整数进行比较。
Dim my_date as Date = DateTime.ParseExact(my_str, "yyyyMMddhhmm", System.Globalization.CultureInfo.InvariantCulture)
回答by Steven Doggart
Normally I wouldn't do this. Several correct answers have already been given, but since you are still learning VB.NET, I feel like a more thorough answer would be helpful to you. So my answer will repeat some of what others have already said.
通常我不会这样做。已经给出了几个正确的答案,但由于您仍在学习VB.NET,我觉得更全面的答案对您有帮助。所以我的回答会重复其他人已经说过的一些内容。
The Int64and Decimaltypes are indeed big enough to to hold a number like that. I suspect that the reason why Convert.ToInt64wasn't working for you is because you were trying to store the results in an Integervariable, like this:
该Int64和Decimal类型确实大到可以把若干个这样的。我怀疑Convert.ToInt64对您不起作用的原因是因为您试图将结果存储在一个Integer变量中,如下所示:
Dim my_str as String = "201308281110"
Dim my_int as Integer = Convert.ToInt64(my_str) ' Throws an OverflowException
The reason that fails is not because ToInt64doesn't work. That part of the statement is actually working fine. The part that's failing is where you are assigning the my_intvariable to the value. my_intis declared as an Integer. The Integertype in VB.NET is actually just a pseudonym for Int32. In other words, it's actually the same thing as this:
失败的原因不是因为ToInt64不起作用。声明的那部分实际上工作正常。失败的部分是您将my_int变量分配给值的地方。 my_int被声明为Integer. IntegerVB.NET 中的类型实际上只是Int32. 换句话说,它实际上与此相同:
Dim my_str as String = "201308281110"
Dim my_int as Int32 = Convert.ToInt64(my_str) ' Throws an OverflowException, just like the above example
To correct the problem, you need to change the type of the my_intvariable to Int64so that it will be big enough to hold the value being returned from the ToInt64function.
要纠正这个问题,您需要将my_int变量的类型更改为,Int64以便它足够大以容纳从ToInt64函数返回的值。
Dim my_str as String = "201308281110"
Dim my_int as Int64 = Convert.ToInt64(my_str) ' Works
In VB.NET, Longis the pseudonym for Int64, so in most cases, that's what you should use. If you are going to use Long, however, using the ToInt64method is a little ugly. It would be easier to read to just use Longon both sides of the assignment, like this:
在 VB.NET 中,Long是 的化名Int64,因此在大多数情况下,您应该使用它。Long但是,如果您打算使用,则使用该ToInt64方法有点难看。只Long在赋值的两边使用会更容易阅读,如下所示:
Dim my_str as String = "201308281110"
Dim my_int as Long = Long.Parse(my_str) ' Works, just like above
That also makes the code marginally safer because it will still work even if the size of Longchanges in the future (however unlikely that may be).
这也使代码稍微更安全,因为即使Long将来更改大小(无论可能性如何),它仍然可以工作。
The Decimaltype would also be large enough to hold the value, but it would be less efficient than using Long, so I wouldn't recommend it.
该Decimal类型也足够大以保存该值,但它的效率会低于使用Long,因此我不推荐它。
This begs the question, however, "Why are you doing this?"If you need to compare the value to another Longvariable, it would make sense to do that, but then how did you get that other Longvalue? If you are converting both Longvalues from strings, then it doesn't make sense to do that. The string is already formatted in a way where it can be easily compared with other strings of the same format. For instance:
然而,这引出了一个问题,“你为什么要这样做?” 如果您需要将值与另一个Long变量进行比较,这样做是有意义的,但是您是如何获得另一个Long值的呢?如果您Long要从字符串转换两个值,那么这样做是没有意义的。该字符串已经以一种可以很容易地与相同格式的其他字符串进行比较的方式进行格式化。例如:
Dim dateTime1 As String = "201308281110"
Dim dateTime2 As String = "201308281850"
If dateTime1 > dateTime2 Then
' Doesn't get here
End If
If dateTime1 < dateTime2 Then
' Get's here
End If
If dateTime1 = dateTime2 Then
' Doesn't get here
End If
If, however, you need to parse the value to read its individual parts (e.g. date, time, year, month, hour), it makes more sense to convert the value to a DateTimevalue. Or, if you need to compare the string value to another value which is already stored in a DateTimevariable, then, in that case, it also makes sense to convert the string to a DateTimevalue. In VB.NET, the pseudonym for DateTimeis simply Date, so in most cases, you should just use that, like this:
但是,如果您需要解析值以读取其各个部分(例如日期、时间、年、月、小时),则将值转换为值更有意义DateTime。或者,如果您需要将字符串值与已存储在DateTime变量中的另一个值进行比较,那么在这种情况下,将字符串转换为DateTime值也是有意义的。在 VB.NET 中,for 的化名DateTime很简单Date,因此在大多数情况下,您应该使用它,如下所示:
Dim my_str as String = "201308281110"
Dim my_date as Date = Date.ParseExact(my_str, "yyyyMMddHHmm", System.Globalization.CultureInfo.InvariantCulture)
If my_date > Date.Now Then
' Do stuff
End If
回答by Tim Schmelter
But Long.Parseworks as well as Convert.ToInt64since it doesn't overflow(Int32.MaxValueis 2147483647):
但Long.Parse效果很好,Convert.ToInt64因为它不会溢出(Int32.MaxValue是 2147483647):
Dim myLong1 = Long.Parse("201308281110")
Dim myLong2 = System.Convert.ToInt64("201308281110")
Note that Longis the same as Int64.
请注意, Long与Int64.

