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)
Related
Using iText7 and documentinfo.GetMoreInfo("ModDate"), I get the following date string:
D:20220817113241+00'00'
How do I parse this in VB.NET?
With the help of the comments, I found a solution:
Initial date string:
D:20220817113241+00'00'
If string starts with "D:", remove it.
20220817113241+00'00'
If a string has a +, remove the part beginning with the +.
20220817113241
Use a full format string like this:
yyyyMMddHHmmsszzz
Now measure the length of the remaining date string.
For example:
Dim s As String = "D:20220817113241+00'00'"
Dim sFormat As String = "yyyyMMddHHmmsszzz"
Dim iLen As Integer = s.Length
sFormat = Mid(sFormat, 0, iLen)
This makes the format string as long as the date string and cuts off anything that is not present in the date string, like "zzz".
Now use that format string like this:
Dim yourDate = DateTime.ParseExact(s, sFormat, Nothing)
Example: The float value is -1580.719 and I need to convert it to hex in VB.NET (Value in hex should output: C4C59704).
Ok, so I found how to do it:
Dim var As Single = Single.Parse("-1580.719")
Dim varArray() As Byte = BitConverter.GetBytes(var)
Array.Reverse(varArray)
Dim result As String = BitConverter.ToString(varArray).Replace("-", "")
Value of result is:
C4C59702
Im trying to Convert Hexadecimal value to String but i dont know how to do.
This what i tried to do but it's Int to Hex.
sHex = Hex(Text)
TextBox2.Text = sHex
Try this
Function StringToHex(ByVal text As String) As String
Dim hex As String
For i As Integer = 0 To text.Length - 1
hex &= Asc(text.Substring(i, 1)).ToString("x").ToUpper
Next
Return hex
End Function
use it with the console.
Debug.WriteLine(StringToHex("sim0n"))
let me know if it works for u.
I have to convert number (double) to string like this:
Dim myDouble = 3.14
Dim myDoubleStr = myDouble.ToString ''OR myDouble.ToString("N")
According to my 'culture' settings result is "3,14" what is in most cases OK.
But here are cases that I need string representation of a number with decimal point instead of comma.
In that case I replace char "," with "." like string manipulation.
Is here a way that "ToString" convert a number with decimal point directly when this is needed?
Try
.ToString("F", CultureInfo.InvariantCulture)
More info here
http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#DFormatString
You can also have as much precession as you want by specifying the format like this:
Dim myDouble As Double = 3.14159268
Dim myDoubleStr = myDouble.ToString("0.00000") 'The value will be 3.14159
In case you wanted to use Thousands separator, use this format:
Dim myDouble = 961327.1234567890
Dim MyDoubleStr = myDouble.ToString("#,##0.00000")
'The value of MyDoubleStr will be 961,327.12345
How do I find last but one character in a vbstring
for e.g. In the string V1245-12V0 I want to return V
Don't use substring to get just one character
Dim MyString As String = "V1245-12V0"
Dim MyChar As Char = MyString(MyString.Length - 2)
Sorry it's been a while since I did VB so this may not be perfect (and is probably a mixture of C# and VB) but you get the idea:
Dim s = "V1245-12V0"
Dim lastButOneLetter = String.Empty
If s.Length > 1 Then
'Can only get the last-but-one letter from a string that is minimum 2 characters
lastButOneLetter = s.Substring(s.Length - 2, 1)
Else
'do something if string is less than 2 characters
End If
EDIT: fixed to be compilable VB.NET code.
Dim secondToLastChar As Char
secondToLastChar = Microsoft.VisualBasic.Strings.GetChar(mystring, mystring.Length - 2)
http://msdn.microsoft.com/en-us/library/4dhfexk4(VS.80).aspx
Or just remember that any string is an array of chars;
secondToLastChar = mystring(mystring.Length - 2)
If you want to get the last alpha-character in a string you could use a LINQ query such as (C#):
var d = from c in myString.ToCharArray().Reverse()
where Char.IsLetter(c)
select c;
return d.First();
string.Substring(string.Length - 2, 1);
Was it difficult?
dim mychar as string
dim yourstring as string
yourstring="V1245-12V0"
mychar=yourstring.Substring(yourstring.Length - 2, 1)
Use the Substring on the string s which contains 'V1245-12V0'
s.Substring(s.Length - 2, 1);
Here's a VB solution:
Dim text = "V1245-12V0"
Dim v = Left(Right(text, 2), 1)
You do not need to check the length of text, except for your semantics as to what you want to happen for empty (and Nothing) and single character strings.
You can have your own functions like
Function Left(ByVal str as string, byval index as integer) As String
Left=str.Substring(0,index);
End Function
Function Right(ByVal str as string, byval index as integer) As String
Right=str.Substring(str.Length-index)
End Function
And use them to get what you need.