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

Discussion in 'C++' started by Zhang Liming, Apr 23, 2007.

  1. Zhang Liming

    Zhang Liming Guest

    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?
    Zhang Liming, Apr 23, 2007
    #1
    1. Advertising

  2. Zhang Liming

    Jim Langston Guest

    "Zhang Liming" <> wrote in message
    news:f0h7sk$sbm$...
    > 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.
    Jim Langston, Apr 23, 2007
    #2
    1. Advertising

  3. Zhang Liming

    Ian Collins Guest

    Jim Langston wrote:
    > "Zhang Liming" <> wrote in message
    > news:f0h7sk$sbm$...
    >
    >>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.
    Ian Collins, Apr 23, 2007
    #3
  4. Zhang Liming

    hamburger Guest

    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 <> wrote:
    > Jim Langston wrote:
    > > "Zhang Liming" <> wrote in message
    > >news:f0h7sk$sbm$...

    >
    > >>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.
    hamburger, Apr 23, 2007
    #4
  5. Zhang Liming

    Guest

    On Apr 22, 10:07 pm, Zhang Liming <> 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'
    , Apr 23, 2007
    #5
  6. Zhang Liming

    Ian Collins Guest

    wrote:
    > On Apr 22, 10:07 pm, Zhang Liming <> 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.
    Ian Collins, Apr 23, 2007
    #6
  7. Zhang Liming

    James Kanze Guest

    On Apr 23, 5:07 am, Zhang Liming <> 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ée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 23, 2007
    #7
  8. "Ian Collins" <> wrote in message
    news:...
    > 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
    Sylvester Hesp, Apr 23, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    568
    Jack Klein
    May 9, 2005
  2. Ioannis Vranos
    Replies:
    11
    Views:
    742
    Ioannis Vranos
    Mar 28, 2008
  3. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    599
    Ben Bacarisse
    Mar 29, 2008
  4. Alex Vinokur
    Replies:
    9
    Views:
    768
    James Kanze
    Oct 13, 2008
  5. pozz
    Replies:
    12
    Views:
    712
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page