Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Converting a string to an const unsigned char*

Reply
Thread Tools

Converting a string to an const unsigned char*

 
 
Hans Mull
Guest
Posts: n/a
 
      04-21-2008
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
 
Reply With Quote
 
 
 
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      04-21-2008
Hans Mull <(E-Mail Removed)> 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__
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      04-21-2008
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());
 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      04-21-2008
On 21 Apr., 17:26, Hans Mull <(E-Mail Removed)> 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
 
Reply With Quote
 
Puppet_Sock
Guest
Posts: n/a
 
      04-21-2008
On Apr 21, 12:32*pm, peter koch <(E-Mail Removed)> wrote:
> On 21 Apr., 17:26, Hans Mull <(E-Mail Removed)> 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
 
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
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
perferred way of converting const char * to const std::string & pookiebearbottom@yahoo.com C++ 2 08-09-2006 06:25 PM
unsigned int const does not match const unsigned int Timo Freiberger C++ 3 10-30-2004 07:02 PM



Advertisments