473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

How do I convert Hexadecimal to String in VB.NET

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.  

8 16397
Luuk
1,047 Expert 1GB
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
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
Luuk
1,047 Expert 1GB
no, try:
Expand|Select|Wrap|Line Numbers
  1. output.Text = Convert.ToInt32(hexadecimalstring, 16).ToString()
  2.  
Dec 1 '15 #4
Actually, that doesn't work. I changes it into 1035. Even if the textbox is empty.
Dec 2 '15 #5
IronRazer
83 64KB
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
Luuk
1,047 Expert 1GB
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
Yes I saw that Luuk, but all it did was put 1035 into the textbox.
Dec 3 '15 #8
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

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

Similar topics

3
by: Eric | last post by:
Guys, I have the following hexadecimal string - '\xff\xff\xff' which i need to convert to binary. How would i go about doing this? Eric
0
by: Mark S Pryor | last post by:
Hello, Using MySQL 4.017 on Win2k sp3: using MySQL built-ins: how can I convert a value stored as a hexadecimal string back to a binary string while logged into the shell? >set @t1=616263;...
2
by: Joel Moore | last post by:
Maybe I'm just easily baffled after an all-nighter but I can't seem to figure out how to represent a BitArray as a hexadecimal string. For example: Dim outputBank As New BitArray(8) ...
3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
4
by: Ken Varn | last post by:
I have an unknown numeric Type object passed into a function. I want to run a conversion on a string to convert the string to that Type object and return an object of that type. Is there some way...
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
6
by: MrKrich | last post by:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has function to do that? I only found Hex function that convert normal integer to Hexadecimal.
3
by: priyanka | last post by:
Hi there, I want to convert a String into integer. I get the string from a file using : string argNum; getline(inputStream,argNum); I now need to convert argNum into integer.
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.