copy BYTE[] to char[]

Posted on 2004-11-17
Last Modified: 2008-02-01
If I have an array of BYTE type and another of char type, how do I copy the BYTE array to the char array?

BYTE buf[255] = "";
char str[255] = "";
//call functions to populate buf


I get a compiler error..
error C2664: 'strcpy' : cannot convert parameter 2 from 'unsigned char [255]' to 'const char *'
Question by:ink777
    LVL 13

    Accepted Solution

    is buf a 0 terminated string?

    The better approach should be
    memcpy (str, buf, 255);
    or if not all bytes need to be copied replace 255 with the correct number.
    LVL 13

    Expert Comment

    Or you can try using a cast:
    strpy (str, (char*) buf);
    LVL 4

    Expert Comment

    Or, more funny :

    char *a = (char *) buf;
    char *b = (char *) str;
    while(*a++ = *b++);

    LVL 3

    Expert Comment

    Casting is the right way to go.  A stickler would point out that we're assuming BYTE and char are the same size.  We should


    just to be sure.

    Also, given that we're assuming they're the same type size and it's just a cast to go from BYTE to char, do you really need to copy the buffer?  If you're going to manipulate it as char and need the original BYTE array unchanged, of course you should do the copy.  But if you're going to be examining it only (read only access), don't spend the time.

    - Frank
    LVL 14

    Expert Comment

    Using strcpy() will only work correctly if  the BYTE array has no embedded NULLs, otherwise only part of the data will be copied.
    LVL 9

    Expert Comment

    strcpy will stop when it sees a zero-byte/null character. use memcpy instead.
    prototype: memcpy(char * destination, char * source, int numbytes);

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now