Convert from ULong to Integer - vb.net

I´m trying to convert a list of ULONG into an array of Integers.
The problem is related to HOW discard the MSB of ULONG! I had tried all kind of operations and all of them are raising an "Overflow Exception" error:
dim MyInteger as Integer = CInt(ULong_Number)
dim MyInteger as Integer = Convert.ToInt32(ULong_Number)
dim MyInteger as Integer = Convert.ToUInt32(ULong_Number)
dim MyInteger as Integer = ULong_Number xor &HFFFFFFFF00000000 xor &HFFFFFFFF00000000
I´m trying to avoid a convertion into a temporary byte array to, after, read each 4 bytes into the integer (the LIST if huge, almost 2 million numbers).
Does anyone have any idea to implicit convert this ULONG to INTEGER?
Thanks!

If you are sure that the value of ULong_Number is not more than the maximum value of an integer (2^31 - 1, or &H7FFFFFFF), or you want to ignore any higher order bits (which would be an odd thing to do, but you are trying to fit an 8-byte number into 4 bytes), you could use this.
Dim MyInteger As Integer = CInt(ULong_Number And CULng(&H000000007FFFFFFF))

Related

Split String in ASCii

I want to split many strings into its ASCII values.
Dim aoC(99), i As Integer
Dim W As String
W="abcd"
For i = 1 To Len(W)
aoC(i) = Asc(Mid(W, i, 1))
Next
Is there a faster method?
You can use
Dim aoC() as Byte
aoC="abcd"
This will give you a byte array of low,high bytes for the 16 bit value for each character in the string. For ascii values the high byte is 0. So to iterate the aoC for the low byte only you would use something like
For myIndex = 0 to ubound(aoC) step 2
Use the locals window to see the raw data.

Unwanted removal of decimal points from list of double after calculation

Using MS VS 2013 (VB.net) with SQL Server 2012.
I am querying a database and returning a list of double using a stored procedure. I am then dividing each double by 8760. When the first list returns from the database it has the decimal places. The list looks like this
After the calc has been performed the decimals have been removed. See image below.
As you can see the decimals are removed. As if you take the first one and divide it by 8760 you get 101.27 Anyone know why or how to avoid this?
My code is as follows
Dim hoursInYear As Double = 8760
Dim steamFees As List(Of Double)
Dim steamFee As Double
Dim steamFeePerHour As New List(Of Double)
Dim steamFeeTotal As Double
steamFees = RunDetailsCalculations.getFixedFeesSteam
For Each steamFee In steamFees
steamFeePerHour.Add(steamFee \ hoursInYear)
Next
steamFeeTotal = steamFeePerHour.Sum
You are using the backslash (\) operator, which is for integer division. Integer division always results in an integer (no fractional part). If you want to retain the fractional part after the division, you need to use floating-point division, which is the forward slash (/) operator.
As the MSDN states:
Integer division is carried out using the \ Operator (Visual Basic). Integer division returns the quotient, that is, the integer that represents the number of times the divisor can divide into the dividend without consideration of any remainder. Both the divisor and the dividend must be integral types (SByte, Byte, Short, UShort, Integer, UInteger, Long, and ULong) for this operator. All other types must be converted to an integral type first.
In other words, when you do this:
Dim result As Double = 887146.6 \ 8760
What you are really doing is this:
Dim input1 As Integer = CInt(887146.6) ' 887146
Dim input2 As Integer = 8760
Dim result1 As Integer = input1 \ input2 ' 887146 \ 8760 = 101 (the remainder is dropped)
Dim result2 As Double = CDbl(result1) ' 101.0D
Or, more simply:
Dim result As Double = CDbl(CInt(887146.6) \ 8760)

How do I make a random character generator? VB.NET

I want to make a random character generator with numbers in vb.net, I know how to make a random number generator but not numbers mixed with letters. I want it to be around 15-20 characters.
Something like this:
F53Gsfdsj637jfsj5kd8
Thanks ahead!
You're mostly there once you have a random number generator. From there, just pick a random character within a collection of valid characters. The simplest way would be something like:
dim validchars as string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
dim sb as new StringBuilder()
dim rand as new Random()
for i as Integer = 1 to 10
dim idx as Integer = rand.Next(0, validchars.Length)
dim randomChar as char = validchars(idx)
sb.Append(randomChar)
next i
dim randomString = sb.ToString()
Of course, clean up the syntax a bit, and maybe use a constant value for the chars and length, a variable value for the number of digits, etc.

How to make unlimited variables (like Num1, Num2, etc)?

I am using vb 2012 express (for desktop), and I was wonder how I can make unlimited variables.
For example
Dim Num1 as integer
Dim Num2 as integer
I want the application to go make a new variable with the Num3,4,5,6 etc.
Is this possible? If so, how?
Consider using and an array or a list:
Dim Num As New List(Of Integer) 'Create a list of integers
For i = 0 To Integer.MaxValue 'Add to the list as much as it can hold which is 2147483647 items, it is integer's maximum value.
Num.Add(0)
Next
'OR
Dim NumArray(Integer.MaxValue) As Integer 'Create an array of integers which holds maximum number of items, again 2147483647 items.
'Youy may access them both via their indexes:
Console.WriteLine(Num(0))
Console.WriteLine(Num(1))
Console.WriteLine(Num(2))
'or
Console.WriteLine(NumArray(0))
Console.WriteLine(NumArray(1))
Console.WriteLine(NumArray(2))
'and so on...
Btw, Console.WriteLine() converts integers to strings in this case.
This is something you need arrays for (or possibly a collection class, depending on other needs).
Something like:
Dim Idx As Integer
Dim Num(10) As Integer
' Now you can use Num(0) thru Num(10) '
For Idx = 0 To 10
Num(Idx) = 10 - Idx
Next

Formatting a Integer in VB

How can I format a integer, 5500000.00 to something like 5,50 Mil.?
How do i convert it or format it?
Thank You.
Yes, you need to convert it to a string to display it that way, but you don't have to convert it to a string to actually get the numeric number of millions. For instance:
Dim total As Integer = 5500000
Dim millions As Decimal = total / 1000000
Dim formatted As String = String.Format("{0} Mil.", millions)
Yes.
You probably don't realize the difference between integer, and it's display (in this case probably decimal) value. 5500000 is string. If you convert it to integer, it is stored somewhere in memory as 00000000 01010011 11101100 01100000 (in bits).
The display value, 5500000, 55.0 Mil are both strings. Computer doesn't know that 5500000 is number - it can only parse the text to it's numerical representation.
Dim value As Integer = 5500000
Dim valueInMil As Single = (CType(value,Single) / 1000000)