• # Thread: Memory problem

1. ## Memory problem

I'm using the following code to write a value of 1 into a specific adress. (Thx to Fleep)
```BYTE Value[] = {0x1,0x0,0x0,0x0};
DWORD BaseAddress = {0xDE77A4};
WriteProcessMemory( hProcHandle, (BYTE*)BaseAddress, &Value, sizeof(Value), NULL);```

How do I write the value 30000 or 12.4567 into this adress?
How would this line
`BYTE Value[] = {0x?,0x?,0x?,0x?};`

look like?

2. BYTE Value[] = {0x1,0x0,0x0,0x0}; means the same as DWORD Value=1;

Check what is "endian"

3. Do you want to address the individual bytes of a 32-bit int? One possible method is a union:
```
union
{
uint32_t integer;
unsigned char byte[4];
} foo;

int main()
{
foo.integer = 123456789;
BYTE Value[] = { foo.byte[3], foo.byte[2], foo.byte[1], foo.byte[0]};
}
```

Or you can do it with pointers (pseudo code ,not sure it works)

```
unsigned char* GetBytes(uint32_t integer){
char[] temp;

for (int i = 0; i < sizeof(integer); ++i) {
unsigned char byte = *((unsigned char *)&x + i);
temp[i] =  (unsigned)byte;
return temp;
}
```

4. I looked up "endian" and after about 10 minutes I ended up in a forum where a nice guy postet this example:

The first need to care about in which machine the program is running because of the difference between Big and Little Ending save bytes in memory.

Big endian saves the most significant byte in lower memory address's, to the opposite of Little endian that saves the less significant byte in lower memoriy address 's.

Example the following integer variable (assuming int = 4 bytes) : 0X1A1B1C1D

in big endian would be saved in memory (assuming address's grow from left to right)
1A | 1B | 1C | 1D
in little endian would be
1D | 1C | 1B | 1A
So in my case it would work like this:
Value to write: 30000 -> 0x7530
`BYTE Value[] = {0x30,0x75,0x0,0x0};`

Can I write it also like this?:
`BYTE Value[] = {0x30,0x75};`

And if I have an adress containing a float number- how could I write that?

Thx guys

5. Originally Posted by Liduen
I looked up "endian" and after about 10 minutes I ended up in a forum where a nice guy postet this example:

So in my case it would work like this:
Value to write: 30000 -> 0x7530
`BYTE Value[] = {0x30,0x75,0x0,0x0};`

Can I write it also like this?:
`BYTE Value[] = {0x30,0x75};`

And if I have an adress containing a float number- how could I write that?

Thx guys
Well you could use the union examplei posted above and instead of uint32_t use a float and replace byte[4] with byte[sizeof(float)]

6. How about some inline asm?

```
void FunctionNameHere()
{
__asm
{
mov eax, 0xDE77A4
mov ecx, 0x7530
mov dword ptr [eax], ecx
}
}

void FunctionNameHere(DWORD base)
{
__asm
{
mov eax, base
mov dword ptr [eax], 0x7530
}
}

void FunctionNameHere(DWORD base, int value)
{
__asm
{
mov eax, base
mov ecx, value
mov dword ptr [eax], ecx
}
}
```

7. Originally Posted by Liduen
I looked up "endian" and after about 10 minutes I ended up in a forum where a nice guy postet this example:

So in my case it would work like this:
Value to write: 30000 -> 0x7530
`BYTE Value[] = {0x30,0x75,0x0,0x0};`

Can I write it also like this?:
`BYTE Value[] = {0x30,0x75};`

And if I have an adress containing a float number- how could I write that?

Thx guys
memset((void*)&Value, 1337.f, sizeof(float));

8. Why not just defining as DWORD and using DWORD Value = 30000; ? Or did I understand sth wrong here?

9. Originally Posted by till0sch97
Why not just defining as DWORD and using DWORD Value = 30000; ? Or did I understand sth wrong here?
Thanks, that made it a lot easier... lol :eek: