Adding a byte to a string

This is a discussion on Adding a byte to a string within the C++ Programming forums, part of the General Programming Boards category; I am playing around with a plugin sdk for a video editor (Wax), and the code is: Code: memset(psImage->psImageData, 0x80, ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    4

    Adding a byte to a string

    I am playing around with a plugin sdk for a video editor (Wax), and the code is:

    Code:
    memset(psImage->psImageData, 0x80, sizeof(ImageSample)*psImage->dwWidth*psImage->dwHeight);
    My understanding of memset is that it replaces the old information with the second argument, for as many times as you specify in the third argument.

    The resulting string(i dont think its actually a string, but I don't know what to call it) is information for an image, with four bytes to each pixel (r, g, b, alpha). sizeof(ImageSample) = 4.

    I want to be able to have a loop and put my own colour. So I need to be able to append four bytes at a time. The for loop looks like this:

    Code:
    for(count = 0; count < psImage->dwWidth*psImage->dwHeight){
    
    }
    But I don't know what to put inside the loop.

    What I think I need is a command that appends a byte to a string or in this case binary information. Thanks.
    Last edited by psul; 04-21-2004 at 06:51 PM. Reason: more appropriate title

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Look up memcpy and memmove. You can easily write code to add to the end of the memory block:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      unsigned char bytes[4];
      char c = '0';
    
      for ( int i = 0; i < 4; i++ ) {
        memcpy ( bytes + i, &c, 1 );
        c++;
      }
      for ( int i = 0; i < 4; i++ ) {
        cout<< bytes[i] <<endl;
      }
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    color model hacks? lol

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    4
    Thanks. I just have a few questions.

    Code:
    memcpy ( bytes + i, &c, 1 );
    Will this access the memory i bytes after the beginning?

    And what is the & in front of the c for?

    My last code crashed, here is the new code:

    Code:
    for(count = 0; count < 4*psImage->dwWidth*psImage->dwHeight; count++){
                    memcpy (psImage->psImageData + count, &c, 1 );
    }
    EDIT: I also have c = '0' above that. I tested the code and it still crashes, im looking for stupid mistakes now.
    Last edited by psul; 04-21-2004 at 07:40 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Will this access the memory i bytes after the beginning?
    It depends on the type of the pointer. If it's a char pointer then yes, otherwise it will access the memory i * sizeof ( type ) after the beginning.

    >And what is the & in front of the c for?
    The second argument requires a pointer. The & operator in this context is the address-of operator, and returns the address of the variable.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    4
    It depends on the type of the pointer. If it's a char pointer then yes, otherwise it will access the memory i * sizeof ( type ) after the beginning.
    I read that a whille ago and didn't quite get it, but now that I read it again the whole thing seems to make sense. Thanks, I am going to try to figure it out again.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    4
    The code works. Thanks so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 05:05 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  5. Adding elements to a string
    By Vicked in forum C Programming
    Replies: 2
    Last Post: 04-10-2003, 01:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21