I'm trying to convert 64bit decimal to hex but I get a wrong result
Public Function DecToBinary(dec As Double) As String
DecToBinary= Hex(dec)
End function
For dec=3689348814742970688 I get 0x3333333333436200 , I should get 0x3333333333436140
I will use the hex to get the binary data cause I couldn't find another way to get a string with the bits.
In the end 3689348814742970688 will become a string with bits
"0011001100110011001100110011001100110011010000110110000101000000"
Thanks in advance
Your assumption that dec contains 3689348814742970688 is wrong.
3689348814742970688 exceeds the precision range of a Double, so dec stores the "rounded" value of 3689348814742970880 instead.
To solve this issue, use a BigInteger from the System.Numerics namespace instead of a Double. It's .NET's data type for arbitrarily large integers.
' prints 3333333333436140
Console.WriteLine(BigInteger.Parse("3689348814742970688").ToString("x"))
This might work,
Dim l As Long = 3689348814742970688L
Dim s As String = Convert.ToString(l, 2)
Debug.WriteLine(s.ToString.PadLeft(64, "0"c))
l = Long.MaxValue
s = Convert.ToString(l, 2)
Debug.WriteLine(s.ToString.PadLeft(64, "0"c))
Related
Need help in converting hex to dec in VB.NET
I use the code below in smaller number which returns the expected result.
leftPaddingHex = Val("&H" & "99000533")
but when using 99000533 it return negative result -1728051917. I am expecting to get 2566915379. I am using to get the correct result in ms sql. using the code below.
CONVERT(BIGINT,CONVERT(varbinary(4),(SELECT master.dbo.fn_cdc_hexstrtobin(#leftPadding))))
Need help on this. Thanks in advance.
Thank you.
Aze
I just thought that I'd post this alternate method.
The various integer type's Parse and TryParse methods allow you to specify a System.Globalization.NumberStyles parameter. You can specify the System.Globalization.NumberStyles.AllowHexSpecifier to parse a hexadecimal string. The only restriction is that the string can not be prefixed with "0x" or "&h".
Dim unsigned32 As UInt32
Dim itParsed As Boolean = UInt32.TryParse("99000533", System.Globalization.NumberStyles.AllowHexSpecifier, Nothing, unsigned32)
The Val method returns Double. This cannot be changed
One workaround would be to use Long data type in the final result (that is, your leftPaddingHex) and to check if the result in the intermediate stage is negative, we add it with UInt.MaxValue + 1 to correct it:
Dim leftPaddingHex As Long = Val("&H" & "99000533")
If leftPaddingHex < 0 Then
leftPaddingHex = leftPaddingHex + UInt32.MaxValue + 1
End If
For hexadecimal with even larger number, I suggest you to take a look on this
I need to convert a 2-byte signed integer into a string of it's hex equivalent, but I need the string to be 4 characters. I've tried the Hex() function but when I convert 0 or say, 10, the result is 0 or A. I need the result to be 0000 or 000A. Any tips or advice?
It is just
Dim hexS As String = i.ToString("X4")
This is well explained in the The Hexadecimal (X) Format specifier.
The 4 after the X specifies how many chars you want in the output and if there are not enough chars, the missing ones are supplied as an appropriate number of "0".
Since you also tagged the question VBA, here is a VBA way to do it
Right("0000" & Hex(i), 4)
Use
Dim i As Integer = 10
Dim hexS As String = i.ToString("X4")
If you are deadset on using Hex as opposed to string formatting then you could use:
Dim Number As Integer
Dim Output As String
Number = 10
Output = ("000" & Hex(Number))
Output = Output.Substring(Output.Length - 4, 4)
Console.WriteLine(Output)
Alternatively make use of string formatting for numbers as so:
Output = Number.ToString("X4")
Console.WriteLine(Output)
The output in both cases with be 000A.
The VB way
Format(i, "X4")
or
Hex(i).PadLeft(4, "0"c)
In Visual Studio 2015:
s = $"{i:X4}"
Alright, so I'm not exactly very oriented when it comes to functions available 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.
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?
Why don't you just simply use Date? You can compare Dates with each other, so there's no need to use an integer for comparing.
Dim my_date as Date = DateTime.ParseExact(my_str, "yyyyMMddhhmm", System.Globalization.CultureInfo.InvariantCulture)
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.
The Int64 and Decimal types are indeed big enough to to hold a number like that. I suspect that the reason why Convert.ToInt64 wasn't working for you is because you were trying to store the results in an Integer variable, like this:
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 ToInt64 doesn't work. That part of the statement is actually working fine. The part that's failing is where you are assigning the my_int variable to the value. my_int is declared as an Integer. The Integer type in VB.NET is actually just a pseudonym for Int32. In other words, it's actually the same thing as this:
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_int variable to Int64 so that it will be big enough to hold the value being returned from the ToInt64 function.
Dim my_str as String = "201308281110"
Dim my_int as Int64 = Convert.ToInt64(my_str) ' Works
In VB.NET, Long is the pseudonym for Int64, so in most cases, that's what you should use. If you are going to use Long, however, using the ToInt64 method is a little ugly. It would be easier to read to just use Long on both sides of the assignment, like this:
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 Long changes in the future (however unlikely that may be).
The Decimal type would also be large enough to hold the value, but it would be less efficient than using Long, so I wouldn't recommend it.
This begs the question, however, "Why are you doing this?" If you need to compare the value to another Long variable, it would make sense to do that, but then how did you get that other Long value? If you are converting both Long values 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:
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 DateTime value. Or, if you need to compare the string value to another value which is already stored in a DateTime variable, then, in that case, it also makes sense to convert the string to a DateTime value. In VB.NET, the pseudonym for DateTime is simply Date, so in most cases, you should just use that, like this:
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
But Long.Parse works as well as Convert.ToInt64 since it doesn't overflow(Int32.MaxValue is 2147483647):
Dim myLong1 = Long.Parse("201308281110")
Dim myLong2 = System.Convert.ToInt64("201308281110")
Demo
Note that Long is the same as Int64.
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)
I have a string which was converted to a char array and stored in a stringcollection, how do I return the numerical value for the character in each string? How can I reverse this process, getting the character from the numerical value?
I know I could code a Select Case statement, but that would take a very long time as I need to cover every character a person could want to conceivably use in the English language, including punctuation.
Is there already a method built into vb.net for doing this?
Thanks for the help!
The Convert class has methods that can convert between characters and integers:
Dim c As Char = "A"C
Dim i As Integer = Convert.ToInt32(c)
Dim c2 As Char = Convert.ToChar(i)
To loop the values converted from the characters in a string into an array of integers:
Dim codes(theString.Length - 1) As Integer
For i As Integer = 0 to theString.Length - 1
codes(i) = Convert.ToInt32(theString.Chars(i))
Next
Try something like this:
For Each c As Char In yourString
Dim i As Int32 = DirectCast(c, Int32)
Next
Remeber that System.String implements IEnumerable<Char> so it is legal to For Each over it. And converting between a character and a number is as simple as casting between System.Char and System.Int32 (here I have shown how to get the numeric value for each character in the string).