Vb.net - 将十六进制值的字符串转换为ASCII [英] Vb.net - converting string of hex values to ASCII

查看:874
本文介绍了Vb.net - 将十六进制值的字符串转换为ASCII的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好!我有一个项目,它基本上使用ReadProcessMemory从正在运行的进程中抓取我的角色名称,并将值返回为Hex。我将此Hex值字符串转换为Ascii字符时遇到问题...示例如下!



我退回的价值:

4d -79-4e-61-6d-65-31-00-00-00

(注意命名约定中的10个字符限制...即:00-00-00)



我真正想要的是:

MyName1





Good Morning! I have a project that basically grabs my character name from a running process using ReadProcessMemory and returns the values as Hex. I'm having trouble converting this string of Hex values to Ascii characters... Example Below!

Value I am returned:
4d-79-4e-61-6d-65-31-00-00-00
(note 10 character limit in naming convention ... ie: 00-00-00)

What I am actually looking for:
MyName1


Dim Name As String = _memManager.ReadString(BaseAddr - &H1D0)







Public Function ReadString(ByVal addr As IntPtr) As String
    'Int32 data type-- is 32 bits long, 4 bytes.
    Dim _dataBytes(10) As Byte
    ReadProcessMemory(_targetProcessHandle, addr, _dataBytes, 10, vbNull)
    Return BitConverter.ToString(_dataBytes, 0)
End Function





我的尝试:



我在网上尝试了几个建议,比如hex2ascii和.tostring,但没有运气!



What I have tried:

I've tried several suggestions online such as hex2ascii and .tostring but no luck!

推荐答案

这取决于你得到的确切内容:如果它是字节4d, 79,4e,......然后它很简单:

It depends on exactly what you are getting: if it's the bytes 4d, 79, 4e, ... then it's fairly simple:
Dim s As String = System.Text.Encoding.UTF8.GetString(_dataBytes)



如果它是一个字符串:4d-79-4e-61-6d-65-31-00-00-00则它更复杂:首先将其转换为字节,然后将字节转换为字符串:


If it's a string: "4d-79-4e-61-6d-65-31-00-00-00" then it's more complex: first convert that to bytes, then convert the bytes to a string:

Public Shared Function GetBytes(value As String) As Byte()
	Return Array.ConvertAll(value.Split("-"C), Function(s) Byte.Parse(s, System.Globalization.NumberStyles.HexNumber))
End Function


我能够通过修改GetBytes函数来解决这个问题



I was able to resolve this issue by modifying the GetBytes function

Public Function ReadString(ByVal addr As IntPtr) As String
    'Int32 data type-- is 32 bits long, 4 bytes.
    Dim _dataBytes(10) As Byte
    ReadProcessMemory(_targetProcessHandle, addr, _dataBytes, 10, vbNull)
    Return System.Text.Encoding.Default.GetString(_dataBytes)
End Function


这篇关于Vb.net - 将十六进制值的字符串转换为ASCII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆