|
-
Sep 15th, 2001, 06:37 PM
#1
Thread Starter
Lively Member
Buffer
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
"Let's all join forces, rule with an iron hand...and prove to all the world, metal rules the land..."
-- Judas Priest
My email is [email protected]
-
Sep 15th, 2001, 08:21 PM
#2
Code:
DWORD dwrd = 0x11223344;
BYTE* buff;
buff = new BYTE[4];
memcpy(buff, &dwrd, 4);
[/code]
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.
-
Sep 15th, 2001, 08:29 PM
#3
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
};
-
Sep 15th, 2001, 08:37 PM
#4
That was actually my first thought, but i've never actually used a union, so i decided to go with what I know =).
Z.
-
Sep 16th, 2001, 01:05 PM
#5
Thread Starter
Lively Member
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
"Let's all join forces, rule with an iron hand...and prove to all the world, metal rules the land..."
-- Judas Priest
My email is [email protected]
-
Sep 16th, 2001, 01:31 PM
#6
Code:
b[0] = HIBYTE(HIWORD(dwrd));
b[1] = LOBYTE(HIWORD(dwrd));
b[2] = HIBYTE(LOWORD(dwrd));
b[3] = LOBYTE(LOWORD(dwrd));
Z.
-
Sep 16th, 2001, 04:49 PM
#7
Thread Starter
Lively Member
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
"Let's all join forces, rule with an iron hand...and prove to all the world, metal rules the land..."
-- Judas Priest
My email is [email protected]
-
Sep 20th, 2001, 04:50 AM
#8
transcendental analytic
to make the simple even simpler
say you want to have a int 12345678 into a[5] in a char[20]
just cast the address to *int
PHP Code:
char a[20];
*(int*)(a+5)=12345678;
this will ofcourse fit into a single mov instruction as it is assignment of a constant
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|