How to store a big number in a variable - vb.net

Nothing works, tried ULong, integer, uint64, biginteger, decimal. how to store this number in the variable? Option strict on
error: Overflow
Dim Number_N As Integer = 115792089237316195423570985008687907853269984665640564039457584007908834671663
Dim Number_N As ULong = 115792089237316195423570985008687907853269984665640564039457584007908834671663
Dim Number_N As UInt64 = 115792089237316195423570985008687907853269984665640564039457584007908834671663
Dim Number_N As BigInteger = 115792089237316195423570985008687907853269984665640564039457584007908834671663
Dim Number_N As Decimal = 115792089237316195423570985008687907853269984665640564039457584007908834671663

You would use BigInteger but you can't represent a BigInteger as a literal as you can with other numeric data types because it's not part of the language. You would have to represent the number in a String literal and call BigInteger.Parse:
Dim Number_N = BigInteger.Parse("115792089237316195423570985008687907853269984665640564039457584007908834671663")

Related

How to zero-extend a signed integer in Visual Basic .NET

For example, I would like to assign &H80000000 to a 64-bit signed integer variable:
Dim a As Long = &H80000000
However, integer a has value &HFFFFFFFF80000000 instead of &H80000000.
I tried to call CULng to circumvent the sign-extension. Nonetheless, it says "Constant expression not representable in type 'ULong'". I conjecture this is because Visual Basic prohibits a negative integer to be assigned to an unsigned variable.
I am using Visual Basic 2010 (.NET Framework 4.0)
Dim a As Long = &H80000000L
Use the literal suffix L to tell the compiler that you intend this value to be a long literal. Otherwise, it's interpreted as a signed integer literal representing a negative value (-2147483648). Alternatively, you can use the suffix UI to denote an unsigned integer.
Example code:
Dim a As Long = &H80000000 ' Int32 literal -2147483648
Dim b As Long = &H80000000L ' Int64 literal 2147483648
Dim c As Long = &H80000000UI ' UInt32 literal 2147483648
Console.WriteLine(a.ToString("X")) ' FFFFFFFF80000000
Console.WriteLine(b.ToString("X")) ' 80000000
Console.WriteLine(c.ToString("X")) ' 80000000

Parsing binary to BigInteger in VB .NET?

How are you?
I wrote a program manipulating big binary chains (string variables). This said manipulation requires me to store my chains in a variable so I can use them as numbers. The only variable type that I have found big enough to store such lengthy numbers is BigInteger (we are talking 1.0E100+).
I would like to use something like:
val = BigInteger.Parse(bin, 2)
But the second parameter needed is a NumberStyles object, which can only refer to a NumberStyles.HexNumber.
Is there a simple/optimal way to do this?
Thank you very much. :)
This converts a binary string to BigInteger in 8 bit chunks. It assumes that the binary string represents a positive number.
Private Function BinToBI(ByRef binstr As String) As BigInteger
Dim t As New List(Of Byte)
Dim s As String
Dim idx As Integer = binstr.Length
Do While idx > 0
'get 8 bits
If idx >= 8 Then
s = binstr.Substring(idx - 8, 8)
Else
s = binstr.Substring(0, idx).PadLeft(8, "0"c)
End If
'convert to byte and add to list
Dim b As Byte = Convert.ToByte(s, 2)
t.Add(b)
idx -= 8
Loop
'force to positive
If t(t.Count - 1) >= 128 Then
t.Add(0)
End If
Dim rv As New BigInteger(t.ToArray)
Return rv
End Function
for testing
Dim d As Double = 1.0E+101
Debug.WriteLine(d.ToString("n2"))
Dim bi As BigInteger
' Dim bin As String = "1111111111111111111111111111111" 'Integer.MaxValue
' Dim bin As String = "111111111111111111111111111111111111111111111111111111111111111" 'Long.MaxValue
Dim bin As String = "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110"
bi = BinToBI(bin)
Debug.WriteLine(bi.ToString("n2"))
This was not well tested but should give you some ideas.

Convert a string to the exact same int in VB.NET

So, I've got a string containing an offset which like this:
Dim myString as String = "&AE6D0"
However, for the BinaryWriter I need it to be an integer like this:
Dim myInt as Integer = &AE6D0
How can I convert myString to myInt?
Remove the "&" sign from the beginning of the string and then convert it like this:
Dim myInt as Integer = Convert.ToInt32("AE6D0", 16)
Source: https://msdn.microsoft.com/en-us/library/vstudio/f1cbtwff%28v=vs.110%29.aspx
(I interpreted your offset as being a hexadecimal value, that's why there is the 16)

VB.NET ULong overflow

I have a timer which returns the time elapsed:
Public Function TimeElapsed() As ULong
Dim ul As ULong
ul = m_lEnd - m_lStart
Dim ul2 As ULong
ul2 = ul - m_lOverhead
Dim ul3 As ULong
ul3 = (ul2 / m_lFreq) * 1000
Return ul3
End Function
Now I experienced the following variables:
m_lEnd = 935083366402
m_lStart = 935007142800
ul2 = 76223588
m_lOverhead = 14
This gives me an overflow in the line
ul3 = (ul2 / m_lFreq) * 1000
I can not see why and how to improve my bug.
Thank you very much for the help!
You need to explicitly tell the compiler you are doing the operation on a ULong... This is because the numeric literals default to Integers, so your trying to use an integer literal which will cause an overflow like your issue. You can force values to different types with the correct suffix. Literal values without a type suffix may only have values up to the range of Long. This case it's ULong so the suffix is: UL.
By the way this is a conversion error, turn Option Strict on...
EX: Dim m_lEnd As ULong = 935083366402UL
Notice this at the end, this is what needs to happen.

VB.net Iterating Structures and setting data types

I am using the following structure many times throughout my code to pass a set of data to a function:
Public Structure MyStruct
Dim ResourceID As String
Dim RSRCName As String
Dim CommercialType As String
Dim ParticipantName As String
Dim ASReserveZone As String
Dim SCADAStatusQuality As String
Dim SCADAStatus As String
Dim SCADAMWQuality As String
Dim ManualDispatchReason As String
Dim RegUpQual As Boolean
Dim RegDownQual As Boolean
Dim SuppQual As Boolean
Dim SpinQual As Boolean
Dim UseEmergencyLimits As Boolean
Dim FollowLastDispatch As Boolean
Dim ASOfferCurveDict As HybridDictionary
Dim XIC As Integer
Dim PriceBased As Integer
Dim PNodeID As Integer
Dim Committed As Integer
Dim CommitmentStatus As Integer
Dim ManualDispatch As Integer
Dim LastApprdNumOfIntervalsToMax As Integer
Dim MWCurve() As Double
Dim EnergyOfferCurve() As Double
Dim PlannedMW As Double
Dim SCADAMW As Double
Dim InitialOnHours As Double
Dim LastApprdDispatchMW As Double
Dim TotalRampedCRDeployMW As Double
End Structure
Dim Struct as MyStruct
I can use Struct = Nothing to reset the data, but I would rather iterate through the structure and set all the numeric values to 999999999. Any idea how to do this?
You could do it with reflection, but I wouldn't necessarily recommend it. I'd sooner just add a Clear or Reset method to the structure which sets all the values to their default values. Another option would be to set the default values when the fields are declared:
Public Structure MyStruct
' ...
Dim XIC As Integer = 999999999
Dim PriceBased As Integer = 999999999
Dim PNodeID As Integer = 999999999
' ...
End Structure
Then, to reset a variable to all the default values, just do this:
myVariable = New MyStruct()
I should also mention, unless it really needs to be a structure for some reason, you should change this to a class. It's quite a lot of data to be passing around on the stack all the time.