CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2005
    Location
    Ellesmera
    Posts
    427

    primitive data question

    hello

    too much use of CString and now I don't know how to use unsigned char anymore....

    just some question/confirmation

    unsigned char data[128];

    1. how to put data? memcpy?
    ex. memcpy(data, "the quick brown fox", 128 ) ---> ??

    2. how to know the actual data length of the data variable??
    *** Con Tu Adios, Te Llevas, Mi Corazon***

    Traveling Encoder...

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: primitive data question

    Maybe this helps.
    Code:
    #include <memory.h>
    #include <string.h>
    
    unsigned char data[128];
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        // using memcpy (be careful with the length, string length + ending zero)
        memcpy(data, "the quick brown fox", 20);
    
        // using strcpy (takes signed char*, not unsigned)
        strcpy((char*) data, "the quick brown fox");
    
        // using sprintf (takes signed char*, not unsigned)
        sprintf((char*) data, "the quick brown fox");
    
        // get size of data
        int datasize = sizeof(data);
    
        // get length of string contained within data (takes signed char*, not unsigned)
        int stringsize = strlen((char*) data);
    
        return 0;
    }
    - petter

  3. #3
    Join Date
    May 2005
    Location
    Ellesmera
    Posts
    427

    Re: primitive data question

    thank you very much
    *** Con Tu Adios, Te Llevas, Mi Corazon***

    Traveling Encoder...

  4. #4
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: primitive data question

    Quote Originally Posted by ideru
    hello

    too much use of CString and now I don't know how to use unsigned char anymore....

    just some question/confirmation

    unsigned char data[128];

    1. how to put data? memcpy?
    ex. memcpy(data, "the quick brown fox", 128 ) ---> ??

    2. how to know the actual data length of the data variable??
    What is the context? What about that CString/unsigned char issue?
    "Programs must be written for people to read, and only incidentally for machines to execute."

  5. #5
    Join Date
    May 2003
    Location
    San Antonio TX
    Posts
    380

    Re: primitive data question

    Quote Originally Posted by ideru

    2. how to know the actual data length of the data variable??
    wildfrog's example is correct, but be careful of the common mistake ...

    Code:
    int EvalStr(char* data);
    
    int main(int argc, char** argv)
    {
         char data[128];
    
         EvalStr(data);
    
         return 0;
    }
    
    int EvalStr(char* data)
    {
        int i = 0;
    
        i = sizeof(data);  /*returns sizeof char *, not allocated size of data*/
    
        strncpy(data, "My useful text information", i-1);
    
        return 0;
    }
    -Kendall
    John 3:16
    For God so loved the world ...

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

Featured