By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,601 Members | 989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,601 IT Pros & Developers. It's quick & easy.

How do I convert Hexadecimal to String in VB.NET

P: 5
I'm making a decoding app, and I made a Hexadecimal decoder, and now I need it to convert into text.
Here is my code to convert: Try
Expand|Select|Wrap|Line Numbers
  1. Sub convertHex()
  2.          Dim byteArray() As Byte
  3.          Dim hexNumbers As System.Text.StringBuilder = New System.Text.StringBuilder
  4.  
  5.          byteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(input.Text)
  6.  
  7.         For i As Integer = 0 To byteArray.Length - 1
  8.          hexNumbers.Append(byteArray(i).ToString("x"))
  9.          Next
  10.          output.Text = hexNumbers.ToString()
  11.     Catch ex As Exception
  12.         MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
  13.     End Try
  14. End Sub
Now how do I convert it back into text?
Nov 29 '15 #1

✓ answered by IronRazer

When you read the string into a byte array the numbers will be between 0 and 255. Those numbers in Hex would be between 0 and FF.

If you notice, they are not always 2 characters long. 0 through 9 would throw it off being you are appending the hex strings one after the other leaving no way to tell where to split the hex values up when you need to convert them back to byte values 0 through 255.

You can fix that by padding the left with zeros "0" so that every Hex value would be 2 characters long. Then, you can read the hex string 2 characters at a time, convert that back to a Byte value and add it to an Array or List of bytes.

Then use the System.Text.Encoding.ASCII.GetString() method to convert the bytes back into a String. It is basically the same process you do to convert the String to Hex except, in reverse.

Try this in a new form project with 3 textboxes and 2 buttons. Type whatever you want in TextBox1 and press Button1. It converts it to Hex and puts it in TextBox2.

Then press Button2 and it will convert the hex back to the same string and put it in TextBox3.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4.         Dim bts() As Byte = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
  5.  
  6.         Dim sb As New System.Text.StringBuilder
  7.         For Each b As Byte In bts
  8.             sb.Append(b.ToString("X").PadLeft(2, "0"c))
  9.         Next
  10.  
  11.         TextBox2.Text = sb.ToString
  12.     End Sub
  13.  
  14.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  15.         Dim bts As New List(Of Byte)
  16.  
  17.         For x As Integer = 0 To TextBox2.TextLength - 1 Step 2
  18.             bts.Add(CByte(Convert.ToInt32(TextBox2.Text(x) & TextBox2.Text(x + 1), 16)))
  19.         Next
  20.  
  21.         TextBox3.Text = System.Text.Encoding.ASCII.GetString(bts.ToArray)
  22.     End Sub
  23. End Class
  24.  

Share this Question
Share on Google+
8 Replies


Expert 100+
P: 1,035
Expand|Select|Wrap|Line Numbers
  1.         Dim hexadecimalstring As String
  2.         For x = 1024 To 1035
  3.             hexadecimalstring = Hex(x).ToString()
  4.             Console.WriteLine("{0} = {1}", hexadecimalstring, Convert.ToInt32(hexadecimalstring, 16).ToString())
  5.         Next x
  6.  
The 'Convert.ToInt32(hexadecimalstring, 16)' will make a integer from the string
Adding '.ToString()' wil make that a string again...
Nov 29 '15 #2

P: 5
I'm using WinForms so would I do
Expand|Select|Wrap|Line Numbers
  1. output.Text = "{0} = {1}", hexadecimalstring, Convert.ToInt32(hexadecimalstring, 16).ToString()
?
Nov 29 '15 #3

Expert 100+
P: 1,035
no, try:
Expand|Select|Wrap|Line Numbers
  1. output.Text = Convert.ToInt32(hexadecimalstring, 16).ToString()
  2.  
Dec 1 '15 #4

P: 5
Actually, that doesn't work. I changes it into 1035. Even if the textbox is empty.
Dec 2 '15 #5

IronRazer
P: 82
When you read the string into a byte array the numbers will be between 0 and 255. Those numbers in Hex would be between 0 and FF.

If you notice, they are not always 2 characters long. 0 through 9 would throw it off being you are appending the hex strings one after the other leaving no way to tell where to split the hex values up when you need to convert them back to byte values 0 through 255.

You can fix that by padding the left with zeros "0" so that every Hex value would be 2 characters long. Then, you can read the hex string 2 characters at a time, convert that back to a Byte value and add it to an Array or List of bytes.

Then use the System.Text.Encoding.ASCII.GetString() method to convert the bytes back into a String. It is basically the same process you do to convert the String to Hex except, in reverse.

Try this in a new form project with 3 textboxes and 2 buttons. Type whatever you want in TextBox1 and press Button1. It converts it to Hex and puts it in TextBox2.

Then press Button2 and it will convert the hex back to the same string and put it in TextBox3.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4.         Dim bts() As Byte = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
  5.  
  6.         Dim sb As New System.Text.StringBuilder
  7.         For Each b As Byte In bts
  8.             sb.Append(b.ToString("X").PadLeft(2, "0"c))
  9.         Next
  10.  
  11.         TextBox2.Text = sb.ToString
  12.     End Sub
  13.  
  14.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  15.         Dim bts As New List(Of Byte)
  16.  
  17.         For x As Integer = 0 To TextBox2.TextLength - 1 Step 2
  18.             bts.Add(CByte(Convert.ToInt32(TextBox2.Text(x) & TextBox2.Text(x + 1), 16)))
  19.         Next
  20.  
  21.         TextBox3.Text = System.Text.Encoding.ASCII.GetString(bts.ToArray)
  22.     End Sub
  23. End Class
  24.  
Dec 3 '15 #6

Expert 100+
P: 1,035
My code line #2
Expand|Select|Wrap|Line Numbers
  1. For x = 1024 To 1035
is just an example to get some numbers, in my case between 1024 and 1035 (inclusive)....

My line #3
Expand|Select|Wrap|Line Numbers
  1. hexadecimalstring = Hex(x).ToString()
converts this number to a hexadecimal string


My line #4
Expand|Select|Wrap|Line Numbers
  1. Console.WriteLine("{0} = {1}", hexadecimalstring, Convert.ToInt32(hexadecimalstring, 16).ToString())
converts this string back to a decimal using
Expand|Select|Wrap|Line Numbers
  1. Convert.ToInt32(hexadecimalstring, 16)
this deciman is converted to a string:
Expand|Select|Wrap|Line Numbers
  1. .ToString()
and finally written to Console, using Console.WriteLine()


which part of this example was unclear?
Dec 3 '15 #7

P: 5
Yes I saw that Luuk, but all it did was put 1035 into the textbox.
Dec 3 '15 #8

P: 5
THANK YOU SO MUCH IronRazer, IT WORKED, and it also helped that you explained it, because I'm not the best at VB.NET. Thank you so much.
Dec 3 '15 #9

Post your reply

Sign in to post your reply or Sign up for a free account.