String as hex

yarmiaanuj

Member
Joined
May 21, 2012
Messages
12
Programming Experience
1-3
I have a string value 'FC' and i want vb.net to assume it as hex so that i can obtain its decimal equivalent '252'. Please help..:miserable:
 
The Convert.ToInt32 method will convert a String to an Integer, allowing you to specify that the String represents a number in base 2, 8, 10 or 16. Convert.ToString will do the opposite. If you don't know for a fact that the String is a valid representation of a hexadecimal number then you can use the Int32.TryParse method and specify the appropriate number style for hex. Please consult the MSDN documentation for the appropriate method first if you have issues and post back if you can't work it out from there.
 
before reading the above reply... i tried the following code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim b As Long = "&H" & TextBox1.Text 'textbox1.text = FC
        Dim a As Integer = b
        TextBox2.Text = CStr(a) 'textbox2.text = 252
    End Sub

i am getting the output as desired, but i feel i am missing something that will create problem in future....please see it and comment...else i still have the above option to try with....
 
You are relying on an implicit conversion and that will only work with Option Strict Off. Unfortunately Option Strict is Off by default but every VB developer should turn it On at the first available opportunity, i.e. the moment they know it exists. Also, with your code, if the user enters an invalid value your app will crash. The suggestions I provided are the proper way to do it so you should use the more appropriate one of those two options. If the data is free input from the user then I'd go with TryParse, which will validate as well as convert.
 
Maybe this was the reason i was unable to rely on it...following your suggestion i designed the following code...again the output is as required and i feel it is a right choice...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As String = TextBox1.Text    'contains a hex number in string form
        Dim b As Integer = Convert.ToInt32(a, 16)
        TextBox2.Text = CStr(b)              'displays the decimal equivalent of hex number in textbox1
    End Sub


i suppose this is going to work fine even if option explicit is turned ON... Please comment
 
No that is not the right choice. As I said before:
If you don't know for a fact that the String is a valid representation of a hexadecimal number then you can use the Int32.TryParse method and specify the appropriate number style for hex.
What is your code going to do if the user types "Hello World" into the TextBox?
 
well in that case, i must specify that the data in textbox1 is coming from a device which is transmitting bytes on comport, which i am receiving and filtering, the data is always hex in form of bytes, for filtering purpose i have converted it to string and have applied several string operations, but at a point i had to display the decimal value of a byte, for that i had to convert that particular byte in hex and get its decimal value.
In no case, the device will transmit "Hello World"...that is why i followed this approach.
I hope now my approach is justified but i would like to have suggestions if they can improve my application....
 
In that case it sounds like your proposed approach would work. The thing is, you shouldn't really be getting the data from the TextBox in that case. The TextBox can only display a String that you provide so you should be using that original String. In fact, you should probably just be working with Bytes, which are numbers in the first place, although exactly how is not clear from the information you've provided.
 
Back
Top