On Apr 23, 5:07 am, Zhang Liming <xphenix...@gmail.com> wrote:
> unsigned char rcd[10];
> rcd contains 10 chars, such as '1', '2'.... etc.
So why is it declared "unsigned char"?
> string str;
> the problem is how to pass the contents in rcd to str with
> minimal cost?
If you know the length, and what to avoid unsightly casts

,
you can use the two argument template constructor for
std::string:
std::string s( rcd, rcd + N ) ;
Otherwise, reinterpret_cast can be used:
std::string s( reinterpret_cast< char const* >( rdc ), N ) ;
, or, if the "string" in rcd is '\0' terminated:
std::string s( reinterpret_cast< char const* >( rdc ) ) ;
But I think the source of your problem is the original
declaration. The usual convensions are:
characters: char
small integers: signed char
raw memory ("bytes" or bits): unsigned char
--
James Kanze (GABI Software) email:
Conseils en informatique orient閑 objet/
Beratung in objektorientierter Datenverarbeitung
9 place S閙ard, 78210 St.-Cyr-l'蒫ole, France, +33 (0)1 30 23 00 34