How to translate (unsigned char []) to string?

Z

Zhang Liming

unsigned char rcd[10];

rcd contains 10 chars, such as '1', '2'.... etc.

string str;

the problem is how to pass the contents in rcd to str with minimal cost?
 
J

Jim Langston

Zhang Liming said:
unsigned char rcd[10];

rcd contains 10 chars, such as '1', '2'.... etc.

string str;

the problem is how to pass the contents in rcd to str with minimal cost?

string str( rcd );
is probably the best way if you can do it.

Otherwise I guess
str = rcd;

is what you need.
 
I

Ian Collins

Jim said:
unsigned char rcd[10];

rcd contains 10 chars, such as '1', '2'.... etc.

string str;

the problem is how to pass the contents in rcd to str with minimal cost?

string str( rcd );
is probably the best way if you can do it.

Otherwise I guess
str = rcd;

is what you need.
Not if rcd is array of *unsigned* char.

If the array is known to be zero terminated, you might be able to get
away with a cast, otherwise you'd have to do something like memcpy to an
array of char and add the terminating '\0'. It all depends what is in
the original array.
 
H

hamburger

Yes, you should confirm there is the terminating '\0' at rcd[9] or
before.
Then you can pass (char *)rcd to the string constructor.
Jim said:
unsigned char rcd[10];
rcd contains 10 chars, such as '1', '2'.... etc.
string str;
the problem is how to pass the contents in rcd to str with minimal cost?
string str( rcd );
is probably the best way if you can do it.
Otherwise I guess
str = rcd;
is what you need.

Not if rcd is array of *unsigned* char.

If the array is known to be zero terminated, you might be able to get
away with a cast, otherwise you'd have to do something like memcpy to an
array of char and add the terminating '\0'. It all depends what is in
the original array.
 
A

adam.madram

unsigned char rcd[10];

rcd contains 10 chars, such as '1', '2'.... etc.

string str;

the problem is how to pass the contents in rcd to str with minimal cost?

xphenix.cn.vcf
1KDownload

wouldn't string concatenation would like

str += rcd;

this would just cause it to be appended to the end of str and then you
can just concatenate another '\0'
 
I

Ian Collins

unsigned char rcd[10];

rcd contains 10 chars, such as '1', '2'.... etc.

string str;

the problem is how to pass the contents in rcd to str with minimal cost?

xphenix.cn.vcf
1KDownload


wouldn't string concatenation would like

str += rcd;

this would just cause it to be appended to the end of str and then you
can just concatenate another '\0'
No it wouldn't in this case because rcd is an array of *unsigned* char.
 
J

James Kanze

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
 
S

Sylvester Hesp

Ian Collins said:
If the array is known to be zero terminated, you might be able to get
away with a cast, otherwise you'd have to do something like memcpy to an
array of char and add the terminating '\0'. It all depends what is in
the original array.

The array doesn't need to be zero-terminated:

std::string s(buffer, buffer + 10);
// or
s.assign(buffer, buffer + 10);

- Sylvester Hesp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top