Converting a string to an const unsigned char*

Discussion in 'C++' started by Hans Mull, Apr 21, 2008.

  1. Hans Mull

    Hans Mull Guest

    Hi!
    How can I convert a string to a const unsigned char*?
    (string::c_str() converts the string to a signed char)

    Thanks in advance, Hans
     
    Hans Mull, Apr 21, 2008
    #1
    1. Advertising

  2. Hans Mull <> writes:

    > Hi!
    > How can I convert a string to a const unsigned char*?
    > (string::c_str() converts the string to a signed char)
    >
    > Thanks in advance, Hans


    #include <algorithm>
    #include <string>

    const unsigned char* convert(const std:string& s){
    unsigned char* bytes=new[s.size()+1]();
    std::copy(s.begin(),s.end(),bytes);
    return(bytes);
    }


    If converting signed chars from the string into unsigned chars poses
    problem, you may use transform instead of copy, passing a function
    that will do the mapping you want between negative characters and
    bytes.

    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, Apr 21, 2008
    #2
    1. Advertising

  3. Hans Mull wrote:
    > Hi!
    > How can I convert a string to a const unsigned char*?
    > (string::c_str() converts the string to a signed char)


    The (bitwise) contents of the string don't change if the characters
    are interpreted as unsigned chars instead of signed chars, so I'd say
    it's completely safe to do this:

    const unsigned char* us =
    reinterpret_cast<const unsigned char*>(str.c_str());
     
    Juha Nieminen, Apr 21, 2008
    #3
  4. Hans Mull

    peter koch Guest

    On 21 Apr., 17:26, Hans Mull <> wrote:
    > Hi!
    > How can I convert a string to a const unsigned char*?
    > (string::c_str() converts the string to a signed char)


    As noted by Juha, a reinterpret_cast is safe and fine, but a question
    is whether you must convert to unsigned char const * (I presume that
    you want to preserve the constness!). Perhaps you should have used a
    std::vector<unsigned char> or a std::basic_string<unsigned char> in
    the first place?

    /Peter
     
    peter koch, Apr 21, 2008
    #4
  5. Hans Mull

    Puppet_Sock Guest

    On Apr 21, 12:32 pm, peter koch <> wrote:
    > On 21 Apr., 17:26, Hans Mull <> wrote:
    >
    > > Hi!
    > > How can I convert a string to a const unsigned char*?
    > > (string::c_str() converts the string to a signed char)

    >
    > As noted by Juha, a reinterpret_cast is safe and fine, but a question
    > is whether you must convert to unsigned char const * (I presume that
    > you want to preserve the constness!). Perhaps you should have used a
    > std::vector<unsigned char> or a std::basic_string<unsigned char> in
    > the first place?


    The usual answer to questions such as that is "legacy code."
    Socks
     
    Puppet_Sock, Apr 21, 2008
    #5
    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. Replies:
    24
    Views:
    907
    Netocrat
    Oct 30, 2005
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,157
    Ian Collins
    May 9, 2006
  3. =?gb2312?B?wNbA1rTzzOzKpg==?=
    Replies:
    26
    Views:
    6,654
    Ian Collins
    Jan 30, 2007
  4. hamishd
    Replies:
    12
    Views:
    5,062
  5. Alex Vinokur
    Replies:
    9
    Views:
    834
    James Kanze
    Oct 13, 2008
Loading...

Share This Page