Forum Discussion
Mike_Thompson_9
Nimbostratus
Aug 08, 2007CommonULong64 OverflowException
I am trying to convert the CommonULong64 type to UInt64 in .NET for a CommonStatistic type. Sometimes this works fine and I get a good number back. Other times I get a System.OverflowException. The CommonULong64 value going in has a high of 4 and a low of -176378626 which does not look right, but that is what is coming back from the SOAP call. Here is my converstion function:
Public Shared Function Build64(ByVal value As Object) As UInt64
Return Convert.ToUInt64(value.high << 32 Or value.low)
End Function
Any ideas?
20 Replies
- Mike_Thompson_9
Nimbostratus
I think I finally have it!
Here is the final version, uncompressed and compressed:
UnCompressed:Public Shared Function Build64(ByVal value As Object) As UInt64 Dim ui64High As UInt64 = (value.high << 32) Dim ui64Low As UInt64 = CLng("&H" & (Hex("&H" & Hex(value.low)))) Return Convert.ToUInt64(ui64High Or ui64Low) End Function
Compressed:Public Shared Function Build64(ByVal value As Object) As UInt64 Return Convert.ToUInt64((value.high << 32) Or CLng("&H" & (Hex("&H" & Hex(value.low))))) End Function
I double checked my numbers whith what is being reported on the F5 and they are correct. Unless someone sees a problem with what I am doing, I am going with it.
Thanks for all the help so far! - Mike_Thompson_9
Nimbostratus
Yeah, Yeah, bang on the VB developer :'( Yes, I know. I do need to drop the VB and move to C at some point, but this app was written in VB so that is what I am going with and what I know (for now). Here are the definitions of the two functions:
CLng http://msdn2.microsoft.com/en-us/library/ck4c5842.aspx
Hex http://msdn2.microsoft.com/en-us/library/8t2d9cx5.aspx
As for efficiency, you are probably correct. However, if there is any slow down the result is trivial in my case.
Basically you are correct. The Hex function returns the number as a hex string, then CLng turns it back in to a Long integer (which is a positive number).
I will keep messing with the code to "optimize" the calculations. If I come up with something better, I will post it. Thanks to both of you for your help.. - Mike_Thompson_9
Nimbostratus
I converted your code to VB (yes, VB) and it does work. Considering speed, do you think your method is faster:Public Shared Function Build64(ByVal value As Object) As UInt64 Dim High As Int32 = value.high Dim Low As Int32 = value.low Dim lowLow As Int32 = value.low And &HFFFF Dim lowHigh As Int32 = (value.low >> 16) And &HFFFF Dim high64 As UInt64 = Convert.ToUInt64(High) Dim lowLow64 As UInt64 = Convert.ToUInt64(lowLow) Dim lowHigh64 As UInt64 = Convert.ToUInt64(lowHigh) Dim final As UInt64 = (high64 << 32) Or (lowHigh64 << 16) Or lowLow64 Return myfinal End Function
Mike - Mike_Thompson_9
Nimbostratus
All "bashing" aside, your help has been invaluable and I cannot thank you enough! Below is the final function that resolved the issue:Public Shared Function Build64(ByVal value As Object) As UInt64 Return (Convert.ToUInt64(value.high) << 32) Or (Convert.ToUInt64((value.low >> 16) And &HFFFF) << 16) Or Convert.ToUInt64(value.low And &HFFFF) End Function
Hopefully this will help others in the VB world with the same problem... - You all absolutely ROCK! This is what community is all about and what makes our little slice of the net so rewarding to us here at DC.
When I get done with Gnomedex (Click here), I'll play around with this and toss something up on my blog highlighting you all - look for it in the podcast next week as well.
-Joe - Don_MacVittie_1Historic F5 Account
Posted By bikemike on 08/10/2007 9:26 AM
All "bashing" aside, your help has been invaluable and I cannot thank you enough! Below is the final function that resolved the issue:Public Shared Function Build64(ByVal value As Object) As UInt64 Return (Convert.ToUInt64(value.high) << 32) Or (Convert.ToUInt64((value.low >> 16) And &HFFFF) << 16) Or Convert.ToUInt64(value.low And &HFFFF) End Function
Hopefully this will help others in the VB world with the same problem...
Mike, looks basically good, but you have one problem... You're word-swapping the low DWORD and not the high DWORD. They both need it because of the way the CPU stores DWORDS. Just do the top in 16 bit chunks just like you did the bottom, and this should be good for any value.
Don. - Mike_Thompson_9
Nimbostratus
So, you are saying I should be doing something like this:Public Shared Function Build64(ByVal value As Object) As String Return FormatNumber((Convert.ToUInt64((value.high >> 16) And &HFFFF) << 16) Or _ Convert.ToUInt64(value.high And &HFFFF) Or _ (Convert.ToUInt64((value.low >> 16) And &HFFFF) << 16) Or _ Convert.ToUInt64(value.low And &HFFFF), 0) End Function
Thanks.... - Mike_Thompson_9
Nimbostratus
I really need to take a moment to review this function and figure out the inner workings. To some extent I understand what is going on, but not fully. That is why I had to keep coming back to you guys for verification of my changes.
In any case, I think we have finally concluded on an acceptable solution and I cannot thank you guys enough! Hopefully this is the last you will hear from me on this one :') - Andy_Herrman_22
Nimbostratus
Doing it in Java should be trivial. The High and Low values are both longs, which are already 64 bits in Java, so no casting is needed. I believe just doing this will work:long value = (high << 32) | low; - Don_MacVittie_1Historic F5 AccountIt's been done in Java for a while, this conversation just made me want to check it over again.
The class for Java is here: http://devcentral.f5.com/Default.aspx?tabid=63&articleType=ArticleView&articleId=78
Don.
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects
