vb.net 如何将十六进制值转换为 ASCII?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14017007/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:31:33  来源:igfitidea点击:

How to convert a hexadecimal value to ASCII?

vb.nethexascii

提问by 43.52.4D.

I am trying to use fstream to write an executable file:

我正在尝试使用 fstream 编写一个可执行文件:

Dim buffer As String
Dim c As Char = Chr(9)
Dim fs As System.IO.FileStream = New System.IO.FileStream("test.exe", IO.FileMode.OpenOrCreate)
Dim w As System.IO.BinaryWriter = New System.IO.BinaryWriter(fs)

w.Seek(0, System.IO.SeekOrigin.Begin)
w.Write(HEX VALUES HERE)

w.Close()
fs.Close()

To convert the hex to ASCII I have tried things such as:

要将十六进制转换为 ASCII,我尝试了以下操作:

  MessageBox.Show(ChrW(Convert.ToInt32("48", 16)))
  MessageBox.Show(Chr(CInt("&H" & "48")))
  MessageBox.Show(Chr(CInt("&H48")))

However each of those functions will only work with a single character. How do I make those functions work for entire strings?

然而,这些功能中的每一个都只能使用一个字符。如何使这些函数适用于整个字符串?

回答by krunal

I am posting another answer. You can use the below code to implement your functionality.

我正在发布另一个答案。您可以使用以下代码来实现您的功能。

Dim st As String = "49204c6f76652050726f6772616d6d696e672e"
    Dim com As String
    For x = 0 To st.Length - 1 Step 2
        Dim k As String = st.Substring(x, 2)
        com &= System.Convert.ToChar(System.Convert.ToUInt32(k, 16)).ToString()
    Next

You can also use the below code function:

您还可以使用以下代码功能:

Dim st As String = "49204c6f76652050726f6772616d6d696e672e"
Dim com As String
For x = 0 To st.Length - 1 Step 2
    com &= ChrW(CInt("&H" & st.Substring(x, 2)))
Next

回答by krunal

You can use this to convert a hexadecimal string to ASCII:

您可以使用它来将十六进制字符串转换为 ASCII:

Dim hexValue = "48"
Dim ASCII_value = System.Convert.ToChar(System.Convert.ToUInt32(hexValue, 16))

回答by Marcus Poli

将 H 转换为十六进制: debug.Print hex(asc("H")) >48 返回再次获取 H: debug.Print chr(val("&H" & "48")) >H

Let me send more details giving a simple example. Remember your answer is how to convert hex to ascii.For it, you just need to get function hex_to_ascii described bellow.

让我通过一个简单的例子发送更多细节。请记住,您的答案是如何将十六进制转换为 ascii。为此,您只需要获取下面描述的函数 hex_to_ascii 。

Sub test()
Dim str_word As Variant
str_word = "Hello Word"
Debug.Print "We've set=" & str_word

Dim hex_word As Variant
hex_word = str_to_hex(str_word)
Debug.Print "Just for convert to Hex format=" & hex_word
'48656C6C6F20576F7264

Dim ascii_word As Variant
ascii_word = hex_to_ascii(hex_word)
Debug.Print "Getting ASCII format from Hex=" & ascii_word
'072101108108111032087111114100

Dim hex_word_from_ascii As Variant
hex_word_from_ascii = ascii_to_hex(ascii_word)
Debug.Print "Finally converting Hex value to ascii=" & hex_word_from_ascii
'48656C6C6F20576F7264

Dim str_from_hex As Variant
str_from_hex = hex_to_str(hex_word_from_ascii)
Debug.Print "Remember=" & str_from_hex
'Hello Word
End Sub

Function str_to_hex(p_str As Variant) As Variant
Dim x As Long
Dim resp As Variant
Dim tmp As String
For x = 1 To Len(p_str)
  tmp = Mid$(p_str, x, 1)
  resp = resp & Hex(Asc(tmp))
Next
str_to_hex = resp
End Function

Function hex_to_str(p_hex As Variant) As Variant
Dim x As Long
Dim resp As Variant
Dim tmp As String
For x = 1 To Len(p_hex) Step 2
  tmp = Mid$(p_hex, x, 2)
  resp = resp & Chr(Val("&H" & tmp))
Next
hex_to_str = resp
End Function

Function hex_to_ascii(p_hex As Variant) As Variant
Dim x As Long
Dim resp As Variant
Dim tmp As String
For x = 1 To Len(p_hex) Step 2
  tmp = Mid$(p_hex, x, 2)
  resp = resp & Format(Val("&H" & tmp), "000")
Next
hex_to_ascii = resp
End Function

Function ascii_to_hex(p_ascii As Variant) As Variant
Dim x As Long
Dim resp As Variant
Dim tmp As String
For x = 1 To Len(p_ascii) Step 3
  tmp = Mid$(p_ascii, x, 3)
  resp = resp & Hex(tmp)
Next
ascii_to_hex = resp
End Function

回答by addohm

Sort of redundant, but I'll add something I find simpler. This is used when you have a byte array.

有点多余,但我会添加一些我觉得更简单的东西。当你有一个字节数组时使用它。

Dim str As String = String.Empty
For Each c In byteArray
    str &= ChrW(c)
Next

回答by Benedikt M.

I've collected various Hex functions and changed them till they worked. Here they are:

我收集了各种十六进制函数并对其进行了更改,直到它们起作用为止。他们来了:

http://pastebin.com/ewQh5iRa

http://pastebin.com/ewQh5iRa

回答by Cary Bondoc

Tried and tested code

尝试和测试的代码

Create a function by copy pasting this

通过复制粘贴创建一个函数

Function HexToString(ByVal hex As String) As String
    Dim text As New System.Text.StringBuilder(hex.Length \ 2)
    For i As Integer = 0 To hex.Length - 2 Step 2
        text.Append(Chr(Convert.ToByte(hex.Substring(i, 2), 16)))
    Next
    Return text.ToString
End Function

Use it like this

像这样使用它

Debug.WriteLine(HexToString("73696D306E"))

Source

来源