Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

vb.net - How to convert a hexadecimal value to ASCII?

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

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:

  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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...