Negative result in HEX to DEC vb.net - vb.net

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

Related

I have a problem converting decimal to hex in vb.net

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))

How do I convert integer to 4-digit hex string?

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}"

using IndexOf in Mid function

Perhaps this is a simple solution for most, but I can't get this to work like it should according to syntax.
I have this line of text "Part Number123456Price$50.00"
I want to pull the part number out of it, so I use this function...
str = Mid(str, str.IndexOf("Part Number") + 12, str.IndexOf("Price"))
My results are str = "123456Price$50.0" every time. I know the part number can vary in length so I need a solid solution of pulling this out.
It can be confusing to mix the legacy VB string methods (such as Mid) with the .Net string methods (like IndexOf). The VB methods use 1 as the index of the first character while the .Net methods use 0.
The following code will extract the part number from a string
Dim str As String = "Part Number123456Price$50.00"
Dim iPart As Integer = str.IndexOf("Part Number") + 11
Dim iPrice As Integer = str.IndexOf("Price")
str = str.Substring(iPart, iPrice - iPart).Trim
The Mid() function of Visual Basic is documented as having three arguments: (1) a string, (2) the beginning location in the string, and (3) the number of characters to copy.
So if your string is "Part Number123456Price$50.00" and you want to pull the part number as a series of digits, the "123456" part of the string, using the Mid() function then you need to find the beginning of the part number digit string and to then know the number of digits.
If your string is in the variable str then you can find the offset by something like str.IndexOf("Number") + len("Number") which will provide the offset to after the string "Number".
Next you need to find the number of digits so you would do something like str.IndexOf("Price") to find where the text "Price" begins and then subtract from that offset the offset of where the digits begin.
The result of all of this is you need a bit of code something like the following. I have not tested this source as I am not a VB programmer so it may need a tweak and you might want to put some checks on data validity as well.
Dim TextNumber as String = "Number"
Dim TextPrice as String = "Price"
iOffset = str.IndexOf(TextNumber) + len(TextNumber)
str = Mid(str, iOffset, str.IndexOf(TextPrice) - iOffset)
Alternatively, if Price is always the format $00.00, this will also work.
Dim str as String = "Part Number123456Price$50.00"
str = str.Remove(str.IndexOf("Price"))

splitting a string to access integer within it

i have a string "<PinX F='53mm'></PinX>", I want to access the 53 within the string and do some addition to it and then add the answer back into that string. I've been thinking about this and wasn't sure whether this can be done with regular expression or not? Can anybody help me out.
thanks
Yes, you can use a regular expression. This will get the digits, parse them to a number, add one to it, and put it back in the string (that is, the result is actually a new string as strings are immutable).
string s = Regex.Replace(
input,
#"(\d+)",
m => (Int32.Parse(m.Groups[1].Value) + 1).ToString()
);
Take a look at the HTML Agility Pack.
A regular expression looks like a good fit for this particular problem:
\d+
Will match one or more digits.
Int32.Parse(Regex.Match("<PinX F='53mm'></PinX>", #"\d+").Value)
Will return 53.
In this single case yes. "'(.*?)' then access the first group, but if this is part of a larger xml regular expressions should not be used. You should utilize the xml parser build into .net find the attribute with xsd and get the value.
Alternatively, here's a small routine...
' Set testing string
Dim s As String = "<PinX F='53mm'></PinX>"
' find first occurence of CHAR ( ' )
Dim a As Integer = s.IndexOf("'")
' find last occurence of CHAR ( ' )
Dim b As Integer = s.LastIndexOf("'")
' get substring "53mm" from string
Dim substring As String = s.Substring(a, b - a)
' get integer values from substring
Dim length As Integer = substring.Length
Dim c As Char = Nothing
Dim result As String = Nothing
For i = 1 To length - 1
c = substring.Chars(i)
If IsNumeric(c) Then
result = result & c
End If
Next
Console.WriteLine(Int32.Parse(result))
Console.ReadLine()

Visual Basic format number to hower many number on left of decimal and 1 decimal without rounding

I am acutally using SSRS but it is for an expression so this is VB code. I am wondering how I would get a number such as 236.4723423 to appear at 236.4 instead of 236.5, so basically I jsut want to truncate it always after 1 decimal.
I tried Format = "N1" this rounds it
I tried Formate = "#######.0" and "######.#" and this rounds it as well.
Any ideas?
value = Math.Floor(value * 10) / 10
Use the format "########.00", then once it's in string form, trim the last char.
Edit:
Dim myString as String
myString = CStr(FORMAT(((SUM(Fields!Shipment_Weight.Value)) / 2000),"######.00"))
myString = myString.Substring(0, myString.Length - 1) & "T"
You will probably want to implement a custom IFormatProvider interface to do this. There is a great example on MSDN here.