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?
- Ahh, the fun that is VB...
Public Function Build64(ByVal value As Object) As UInt64 Dim ui64High As UInt64 = value.high Dim ui64Low As UInt64 = value.low ui64High = ui64High << 32 Return Convert.ToUInt64(ui64High Or ui64Low) End Function
public UInt64 build64(CommonULong64 ul64) { return (((UInt64)(UInt32)ul64.high) << 32) | ((UInt64)(UInt32)ul64.low); }
- Mike_Thompson_9
Nimbostratus
Thanks Joe, but I am still getting the same results. In other words, I am still getting the OverflowException. I used both the function you provided and the condensed version below (both with the same results):Public Shared Function Build64(ByVal value As Object) As UInt64 Return Convert.ToUInt64((Convert.ToUInt64(value.high) << 32) Or Convert.ToUInt64(value.low)) End Function
- Mike_Thompson_9
Nimbostratus
I did figure out this: - Mike_Thompson_9
Nimbostratus
There appears to be a problem with the large negative number on the [value.low]. From doing some research, the best method is to turn the negative number in to a positive number, then do the calculation. From what I can tell, this works:Public Shared Function Build64(ByVal value As Object) As UInt64 Return Convert.ToUInt64((Convert.ToUInt64(value.high) << 32) Or Convert.ToUInt64(Math.Abs(value.low))) End Function
- Andy_Herrman_22
Nimbostratus
I worry your low value will be wrong using ABS.Public Function Build64(ByVal value As Object) As UInt64 Dim ui64High As UInt64 = value.high Dim ui64Low As UInt64 = (value.low And 0x00000000FFFFFFFF) ui64High = ui64High << 32 Return Convert.ToUInt64(ui64High Or ui64Low) End Function
- Andy_Herrman_22
Nimbostratus
Looking back at your previous posts, you probably need to do use Convert.ToUInt64() in a couple places there for it to work. - Mike_Thompson_9
Nimbostratus
For the life of me I cannot get this figured out. It all has to do with that negative number coming back for the value.low and converting it to a UInt64. Any other CommonULong64 which does not contain a negative number works fine. - I've been working on this as well and have been having the same issues as you have. Any time a conversion from your -176378626 to a unsigned integer is causing an overflow. -176378626 should equate to 4118588670 as a unsigned 32 bit value but it seems however I try, I can't get VB to do that conversion. C handle it fine with it's dynamic casting, but for some reason VB keeps overflowing.
high = 4, low = -176378626 (4118588670)
Dim ui64 As UInt32 = Convert.ToUInt32(-1)
- Mike_Thompson_9
Nimbostratus
Yeah, that was my thought exactly. Write just the piece that does the conversion in C, then worry about figuring the solution in VB later. At least this would get me going. However, I am certain this can be done in VB, just have to muddle through it to get the solution. - Andy_Herrman_22
Nimbostratus
Another option would be to split the low value into two positive ints (basically, getting a lowLow and lowHigh value).int32 high = value.high; int32 low = value.low; int32 lowLow = value.low & 0x0000FFFF; int32 lowHigh = (value.low >> 16) & 0x0000FFFF; uint64 high64 = Convert.ToUInt64(high); uint64 lowLow64 = Convert.ToUInt64(lowLow); uint64 lowHigh64 = Convert.ToUInt64(lowHigh); uint64 final = (high64 << 32) | (lowHigh64 << 16) | lowLow64; return final;
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