Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


What I have tried:

I've tried several suggestions online such as hex2ascii and .tostring but no luck!
Posted
Updated 21-Apr-17 9:00am
v2

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

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:
VB
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
 
Share this answer
 
Comments
Member 13143502 21-Apr-17 12:45pm    
The value that's returned from my function:
Dim Name As String = _memManager.ReadString(BaseAddr - &H1D0)


Returns this value:
4A-6F-65-4D-61-6D-6D-61-44-58-00

Actual Text:
JoeMammaDX
OriginalGriff 21-Apr-17 13:35pm    
So pass it to the GetBytes function I gave you, and then convert it to a string with the GetString.
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900