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 *'
ink777Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteHCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteHCommented:
Or you can try using a cast:
strpy (str, (char*) buf);
0
GlomCommented:
Or, more funny :

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

:)
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

esorfCommented:
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
0
waysideCommented:
Using strcpy() will only work correctly if  the BYTE array has no embedded NULLs, otherwise only part of the data will be copied.
0
jhshuklaCommented:
strcpy will stop when it sees a zero-byte/null character. use memcpy instead.
prototype: memcpy(char * destination, char * source, int numbytes);
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.