?
Solved

copy BYTE[] to char[]

Posted on 2004-11-17
9
Medium Priority
?
646 Views
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

strcpy(str,buf);

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

Accepted Solution

by:
SteH earned 200 total points
ID: 12602817
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
 
LVL 13

Expert Comment

by:SteH
ID: 12602832
Or you can try using a cast:
strpy (str, (char*) buf);
0
 
LVL 4

Expert Comment

by:Glom
ID: 12604453
Or, more funny :

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

:)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:esorf
ID: 12604628
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
 
LVL 14

Expert Comment

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

Expert Comment

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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

578 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