On Apr 23, 5:07 am, Zhang Liming <xphenix...@gma il.comwrote:
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_cas t can be used:
std::string s( reinterpret_cas t< char const* >( rdc ), N ) ;
, or, if the "string" in rcd is '\0' terminated:
std::string s( reinterpret_cas t< 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:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34