473,320 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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?

Apr 23 '07 #1
7 21867
"Zhang Liming" <xp********@gmail.comwrote in message
news:f0**********@news.yaako.com...
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.
Apr 23 '07 #2
Jim Langston wrote:
"Zhang Liming" <xp********@gmail.comwrote in message
news:f0**********@news.yaako.com...
>>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.

--
Ian Collins.
Apr 23 '07 #3
Yes, you should confirm there is the terminating '\0' at rcd[9] or
before.
Then you can pass (char *)rcd to the string constructor.
On 4ÔÂ23ÈÕ, ÉÏÎç11ʱ44·Ö, Ian Collins <ian-n...@hotmail.comwrote:
Jim Langston wrote:
"Zhang Liming" <xphenix...@gmail.comwrote in message
news:f0**********@news.yaako.com...
>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.

--
Ian Collins.

Apr 23 '07 #4
On Apr 22, 10:07 pm, Zhang Liming <xphenix...@gmail.comwrote:
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'

Apr 23 '07 #5
ad*********@gmail.com wrote:
On Apr 22, 10:07 pm, Zhang Liming <xphenix...@gmail.comwrote:
>>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.

--
Ian Collins.
Apr 23 '07 #6
On Apr 23, 5:07 am, Zhang Liming <xphenix...@gmail.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_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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 23 '07 #7

"Ian Collins" <ia******@hotmail.comwrote in message
news:59**************@mid.individual.net...
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.

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

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

- Sylvester Hesp
Apr 23 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Michael B Allen | last post by:
Hello, Early on I decided that all text (what most people call "strings" ) in my code would be unsigned char *. The reasoning is that the elements of these arrays are decidedly not signed. In...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.