.net
BitConverter class.
Code:
byte[] theBytes = ReadMemory(0x007CE020);
int theAddr = BitConvert.ToInt(theBytes); // theAddr = 0x5193BD6C
"Because my function reads the address value as int not as byte[]."
^^make 1 functions for each data type you expect to read: Int, UInt, Short, UShort, Int64, etc etc. so you're getting returned a core data type, not bytes.
Code:
public static Int32 ReadMemory(Process process, int address, out int bytesRead)
{
IntPtr hProc = OpenProcess(ProcessAccessFlags.All, false, proces*****);
byte[] buffer = new byte[4];
ReadProcessMemory(hProc, new IntPtr(address), buffer, 4, out bytesRead);
return BitConvert.ToInt32(buffer);
}
public static Int64 ReadMemory(Process process, int address, out int bytesRead)
{
IntPtr hProc = OpenProcess(ProcessAccessFlags.All, false, proces*****);
byte[] buffer = new byte[8];
ReadProcessMemory(hProc, new IntPtr(address), buffer, 8, out bytesRead);
return BitConvert.ToInt62(buffer);
}
public static UInt32 ReadMemory(Process process, int address, out int bytesRead)
{
IntPtr hProc = OpenProcess(ProcessAccessFlags.All, false, proces*****);
byte[] buffer = new byte[4];
ReadProcessMemory(hProc, new IntPtr(address), buffer, 4, out bytesRead);
return BitConvert.ToUInt32(buffer);
}
^^there is still room for improvement (IntPtr?), but hopefully that gives you ideas.
edit: you were already using BitConverter, so..
If, for debugging, you want to see the number in hex, just call .ToString("X") which will format the string as hex, not decimal.
ie. int someNum = 100; string myStr = someNum.ToString("X"); // myString = "64"
ie. int someNum = 196; string myStr = someNum.ToString("X"); // myString = "C4"
Code:
byte[] theBytes = ReadMemory(0x007CE020);
int theAddr = BitConvert.ToInt(theBytes);
MsgBox(theAddr.ToString("X"));
- in your original idea "If so, how can I convert an hexadecimal value as byte array"
it's not a hex value, it's just a value
-- it's a byte array, it's not "stored" in some specific representation like hex,decimal,binary etc.. in reality, it's all the same value, so no conversion required.
99% of the time, leave your addresses as Int (should be IntPtr), you should never have to convert to hex. Except, maybe to debug/test, because for example, CE displays addr in hex, not decimal.
repeat: .ToString("X"); for DISPLAYING in Hex.
.
As humans, we default to base 10...so when we say "34" it's kind of implied that we mean base 10
..However, because it's less digits to look at, sometimes we *view* the number in a different base(CE: hex), but really it still has exactly the same value.
When you display a number in c#, like an int, you're actually calling .ToString() on that int! Which makes sense because, INT is a NUMBER, but we're trying to display it as a "string"..so it has to convert it's "value" to a string --> by default, again, because humans like base 10, it uses base 10 and shows the string of "34". (22 in hex)
So when you put MsgBox(someInt), it's actually saying MsgBox(someInt.ToString(decimal))
and if you're trying to see the same addr as CE displays, you probably want
MsgBox(someInt.ToString("X")); //in hex
and to avoid confusion, any time you debug/output a value in hex, prefix it with "0x" so you know it's being displayed in hex

ie. msgBox("0x" + someInt.ToString("X")); // hex, with prefix, so we know* it's displayed in hex.