Hey Guys
Does anyone know how to copy a DWORD into a BYTE Array i guess you would have to split the DWORD into each byte then write them into the BYTE array one at a time but i dont know how to do this any help would be great.
Cheers
Peter Liddle
Printable View
Hey Guys
Does anyone know how to copy a DWORD into a BYTE Array i guess you would have to split the DWORD into each byte then write them into the BYTE array one at a time but i dont know how to do this any help would be great.
Cheers
Peter Liddle
[/code]Code:DWORD dwrd = 0x11223344;
BYTE* buff;
buff = new BYTE[4];
memcpy(buff, &dwrd, 4);
You will be able to get each byte from buff (ie, buff[0] == 0x44). Beware, the bytes are backwards from the way you assigned them. Intel Architecture at work.
Z.
Zaei's code is straightforward, but it'd be simpler if you created a union. This is why they were invented.
PHP Code:union MyType // Declare a union that can hold the following:
{
DWORD lValue; // long value
BYTE cValue[4]; // char array
};
That was actually my first thought, but i've never actually used a union, so i decided to go with what I know =).
Z.
cheers guys
is there anyway of doing it without using existing functions ie. not using mscpy() i mean how would it actually work copying the DWORD to a buffer. The problem is the mscpy command doesn't work properly for what i need because as soon as i add characters to the buffer it over writes the data from the dwords.
cheers
Z.Code:b[0] = HIBYTE(HIWORD(dwrd));
b[1] = LOBYTE(HIWORD(dwrd));
b[2] = HIBYTE(LOWORD(dwrd));
b[3] = LOBYTE(LOWORD(dwrd));
Cheers Z
Thats what i needed to know and amazingly the basic c is what i prefer programming with rather then all these pre written functions. I guess i liked to know whats going on.
Pete
say you want to have a int 12345678 into a[5] in a char[20]
just cast the address to *int
this will ofcourse fit into a single mov instruction as it is assignment of a constantPHP Code:char a[20];
*(int*)(a+5)=12345678;