Results 1 to 8 of 8

Thread: Buffer

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    England
    Posts
    94

    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]

  2. #2
    Zaei
    Guest
    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.

  3. #3
    jim mcnamara
    Guest
    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
    }; 

  4. #4
    Zaei
    Guest
    That was actually my first thought, but i've never actually used a union, so i decided to go with what I know =).

    Z.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    England
    Posts
    94
    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]

  6. #6
    Zaei
    Guest
    Code:
    b[0] = HIBYTE(HIWORD(dwrd));
    b[1] = LOBYTE(HIWORD(dwrd));
    b[2] = HIBYTE(LOWORD(dwrd));
    b[3] = LOBYTE(LOWORD(dwrd));
    Z.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    England
    Posts
    94
    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]

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Smile 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
  •  



Click Here to Expand Forum to Full Width