Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to translate (unsigned char []) to string?

Reply
Thread Tools

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

 
 
Zhang Liming
Guest
Posts: n/a
 
      04-23-2007
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?

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      04-23-2007
"Zhang Liming" <(E-Mail Removed)> wrote in message
news:f0h7sk$sbm$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-23-2007
Jim Langston wrote:
> "Zhang Liming" <(E-Mail Removed)> wrote in message
> news:f0h7sk$sbm$(E-Mail Removed)...
>
>>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.
 
Reply With Quote
 
hamburger
Guest
Posts: n/a
 
      04-23-2007
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 <(E-Mail Removed)> wrote:
> Jim Langston wrote:
> > "Zhang Liming" <(E-Mail Removed)> wrote in message
> >news:f0h7sk$sbm$(E-Mail Removed)...

>
> >>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.



 
Reply With Quote
 
adam.madram@gmail.com
Guest
Posts: n/a
 
      04-23-2007
On Apr 22, 10:07 pm, Zhang Liming <(E-Mail Removed)> wrote:
> 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'

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-23-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Apr 22, 10:07 pm, Zhang Liming <(E-Mail Removed)> wrote:
>
>>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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-23-2007
On Apr 23, 5:07 am, Zhang Liming <(E-Mail Removed)> 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:(E-Mail Removed)
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

 
Reply With Quote
 
Sylvester Hesp
Guest
Posts: n/a
 
      04-23-2007

"Ian Collins" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments