Link to home
Start Free TrialLog in
Avatar of ink777
ink777

asked on

copy BYTE[] to char[]

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

strcpy(str,buf);

I get a compiler error..
error C2664: 'strcpy' : cannot convert parameter 2 from 'unsigned char [255]' to 'const char *'
ASKER CERTIFIED SOLUTION
Avatar of Member_2_1001466
Member_2_1001466

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Member_2_1001466
Member_2_1001466

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

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

:)
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

ASSERT(sizeof(BYTE)==sizeof(char));

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
Using strcpy() will only work correctly if  the BYTE array has no embedded NULLs, otherwise only part of the data will be copied.
strcpy will stop when it sees a zero-byte/null character. use memcpy instead.
prototype: memcpy(char * destination, char * source, int numbytes);