Thread: Writing structure in a unsigned char array

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    5

    Writing structure in a unsigned char array

    I want to write the 2 structures of the bmp file into a buffer of unsigned char.Infact i am trying to write the whole bitmap into a buffer of usigned char, including headers and data. Can any one tell me how it is possible?


    I am trying to write the following structues in the memory allocated as "unsigned char Bitmap[IHeader.BiSizeImage];

    ////////////////////////Structure for the BMP file header/////////

    struct BmpFileHeader
    {
    unsigned short BfType;
    unsigned long BfSize;
    unsigned short BfReserved1;
    unsigned short BfReserved2;
    unsigned long BfOffBits;

    };



    ///////////////////////Structure for the BMP Image Header/////////

    struct BmpImageHeader
    {
    unsigned long BiSize;
    unsigned long BiWidth;
    unsigned long BiHieght;
    unsigned short BiPlanes;
    unsigned short BiBitCount;
    unsigned long BiCompression;
    unsigned long BiSizeImage;
    unsigned long BiXPixPerMeter;
    unsigned long BiYPixPerMeter;
    unsigned long BiColorUsed;
    unsigned long BiColorImportant;
    };

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Why as an unsigned char buffer, and not as a structure buffer? Oh well, it's your decision .
    Code:
    typedef struct 
    {
       unsigned short BfType; 
       unsigned long BfSize; 
       unsigned short BfReserved1; 
       unsigned short BfReserved2; 
       unsigned long BfOffBits; 
    }BmpFileHeader;
    
    typedef struct 
    {
       unsigned long BiSize; 
       unsigned long BiWidth; 
       unsigned long BiHieght; 
       unsigned short BiPlanes; 
       unsigned short BiBitCount; 
       unsigned long BiCompression; 
       unsigned long BiSizeImage; 
       unsigned long BiXPixPerMeter; 
       unsigned long BiYPixPerMeter; 
       unsigned long BiColorUsed; 
       unsigned long BiColorImportant; 
    }BmpImageHeader;
    
    int main()
    {
       unsigned char* FileBuffer;
       unsigned char* ImageBuffer;
    
       FileBuffer = new unsigned char[sizeof(BmpFileHeader)];
       if(FileBuffer == NULL)
       {
          return 0;
       }
    
       ImageBuffer = new unsigned char[sizeof(BmpImageHeader)];
       if(ImageBuffer == NULL)
       {
          delete[] FileBuffer;
          return 0;
       }
    
       ...
    
       delete[] FileBuffer;
       delete[] ImageBuffer;
       retun 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM