How do I convert a floating point to hexadecimal in VB.NET? - vb.net

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

Related

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)

String to Hex Conversion Using Vb.Net

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.

Converting Double to String with desired decimal separator

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

Converting String to String of Hex and vice-versa in Vb.Net

I need to convert a String of totally random characters in something i can read back!
My idea is:
Example String: hi
h (Ascii) -> 68 (hex)
i (Ascii) -> 69 (hex)
So converting hi i must have 6869
My value is now in Base64 (i got it with a Convert.ToBase64String()), is this "ascii to hex" conversion correct? In base64 i have value like "4kIw0ueWC/+c=" but i need characters only, special characters can mess my system
The vb.net Convert can only translate to base64 string :(
edit: This is my final solution:
i got the base64 string inside my enc variable and converted it first in ASCII then in corrispondent Hex using:
Dim bytes As Byte() = System.Text.Encoding.ASCII.GetBytes(enc)
Dim hex As String = BitConverter.ToString(bytes).Replace("-", String.Empty)
After that i reversed this with:
Dim b((input.Length \ 2) - 1) As Byte
For i As Int32 = 0 To b.GetUpperBound(0)
b(i) = Byte.Parse(input.Substring(i * 2, 2), Globalization.NumberStyles.HexNumber)
Next i
Dim enc As New System.Text.ASCIIEncoding()
result = enc.GetString(b)
After all this i got back my base64string and converted one last time with Convert.FromBase64String(result)
Done! Thanks for the hint :)
First get Byte() from your base64 string:
Dim data = Convert.FromBase64String(inputString)
Then use BitConverter:
String hex = BitConverter.ToString(data)

Converting a decimal to an empty string in vb.net

I'm sure this is simple to do but I'm struggling to get this work, I've tried using convert.tostring, decimal.tostring, ctype(object, type) and cstr(object) but without success. I guess I'm trying to change the decial object to a string object and then assign it a value of empty string but always get type mismatch error.
Dim testdecimal as decimal = 0.0
testdecimal = Cstr(testdecimal)
testdecimal = string.empty
Your variable is a Decimal.
It cannot hold a string.
You need to declare a separate variable As String to hold the string.
Although you can't set the value of a decimal variable to String.Empty, you can do something like this...
Dim TestDecimal As Decimal = 0.0
Dim strStringValue As String = IIf(TestDecimal = 0.0, "", TestDecimal.ToString())
MsgBox(strStringValue)
You can't convert a decimal to an empty string. I don't know why you would need to do the above, but I would use an object instead:
Dim test As Object = 10.12345
test = "Hi"
test = String.Empty;
It looks like you just need to create a string representiation of the number, which you can do like this:
'create a decimal variable
Dim testDec As Decimal = 10.12345
'convert decimal to string and then set to empty string
Dim testStr As String = testDec.ToString("N")
testStr = String.Empty
Your problem here is that CSTR converts the value to string and not the object itself so what you are doing is taking a decimal variable then converting its value to string and trying to put the string back into the decimal.
Dim testdecimal as decimal = 0.0 'testDecimal is a decimal type and you are assigning a decimal value
testdecimal = Cstr(testdecimal) 'testDecimal is still a decimal but you are trying to put a string in it Here is your first type mismatch
testdecimal = string.empty ' If this actually had worked it would have made the above line pointless because you just tried to overwrite the value (even though this line did not execute here is your second type mismatch)
What you would need to do is:
Dim NewString as String
Dim testdecimal as decimal = 0.0
NewString = Cstr(testdecimal)
The above takes the decimal value and converts it to string then stores it into a string variable.
Now for the second part of your problem, converting a decimal to an empty string. This is impossible because 0.0 converted to string is still "0.0" string.Empty is "" its just an empty string.
If what you mean is you want to convert a decimal to string BUT if the value is 0.0 then make an empty string you can easily do that with an IF statement.
Basically just do:
Dim NewString as String
Dim testdecimal as decimal = 0.0
if(testdecimal =0.0) Then
NewString = String.Empty
Else
NewString = Cstr(testdecimal)
END IF