【问题标题】:How to convert a hexadecimal value to ASCII?如何将十六进制值转换为 ASCII?
【发布时间】:2012-12-10 14:52:24
【问题描述】:

我正在尝试使用 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()

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

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

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

【问题讨论】:

    标签: vb.net hex ascii


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:
      将 H 转换为十六进制: 调试。打印十六进制(asc(“H”)) >48 再次返回获得 H: debug.Print chr(val("&H" & "48")) >H

      让我发送更多详细信息,举一个简单的例子。 记住你的答案是如何将十六进制转换为 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
      

      【讨论】:

      • 请把你的答案解释得更详细,以便其他人理解。
      【解决方案3】:

      久经考验的代码

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

      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
      

      这样使用

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

      Source

      【讨论】:

        【解决方案4】:

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

        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
        

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

        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
        

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

          • 也许你完全错过了我的问题的重点......我该如何处理多个字符?如果我说 "Dim hexValue = "48&49" 它不起作用......如果我在两者之间添加一个空格它仍然不起作用......我如何让它适用于整个字符串?(或者只是两个十六进制字节,例如“48 49”)
          • @43.52.4D。您需要创建自己的方法并解析字符串以构建您期望的 ascii 结果
          • 我知道你想做什么。使用此示例代码将 HEX 转换为 ASCII 非常简单。我正在给你提示以解决这个问题。您应该使用 For Loop 从 HEX 生成 ASCII。上面我的回答是正确的,它可以工作并为您提供 ASCII 值。
          • 嘿,谢谢,但我已经像你之前的答案一样编写了代码......它工作正常,除非你输入一个十六进制值,例如“80”......它会写它作为“3F”,即“?”我该如何解决这个问题?
          • 你用“80”和“3F”转换成ASCII了吗?
          【解决方案6】:

          我收集了各种 Hex 函数并对其进行了更改,直到它们起作用为止。 他们在这里:

          http://pastebin.com/ewQh5iRa

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-09-27
            • 1970-01-01
            • 1970-01-01
            • 2019-02-13
            • 2013-10-02
            • 2017-08-28
            • 2017-10-13
            • 2011-11-21
            相关资源
            最近更新 更多