如何在 VB.NET 中将 ASCII 转换为十六进制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25129402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert ASCII to hexdecimal in VB.NET?
提问by Eden_Pierce
I want to make a program that converts an ASCIIstring to hexadecimal in VB.NET.
我想在VB.NET中制作一个将ASCII字符串转换为十六进制的程序。
This is what I'm talking about:
这就是我要说的:
There is a form with two textboxes and one button. The user puts in something in ASCII in textbox1, and after hitting the button, textbox2 displays it in hexadecimal.
有一个带有两个文本框和一个按钮的表单。用户在 textbox1 中输入 ASCII 码,点击按钮后,textbox2 以十六进制显示。
So input in ASCII on textbox 1: test
所以在文本框 1 上输入 ASCII:测试
Output in hexadecimal in textbox2 after hitting button1: 74657374
点击 button1 后在 textbox2 中以十六进制输出:74657374
Is there a way to do this?
有没有办法做到这一点?
采纳答案by Eden_Pierce
The code that convert string to hex value
将字符串转换为十六进制值的代码
Dim str As String = "test"
Dim byteArray() As Byte
Dim hexNumbers As System.Text.StringBuilder = New System.Text.StringBuilder
byteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(str)
For i As Integer = 0 To byteArray.Length - 1
hexNumbers.Append(byteArray(i).ToString("x"))
Next
MsgBox(hexNumbers.ToString()) ' Out put will be 74657374
The following code will reverse the operation and verify the output:
以下代码将反转操作并验证输出:
Dim st As String = hexNumbers.ToString
Dim com As String = ""
For x = 0 To st.Length - 1 Step 2
com &= ChrW(CInt("&H" & st.Substring(x, 2)))
Next
MsgBox(com)
回答by Luke Hoffmann
textbox2.Text = ""
For Each c As Char In textbox1.Text
textbox2.Text &= Convert.ToString(Convert.ToInt32(c), 16)
Next

