468,294 Members | 1,834 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,294 developers. It's quick & easy.

Converting a string to an const unsigned char*

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
Jun 27 '08 #1
5 11436
Hans Mull <ha********@aol.dewrites:
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__
Jun 27 '08 #2
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());
Jun 27 '08 #3
On 21 Apr., 17:26, Hans Mull <hans72m...@aol.dewrote:
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 charor a std::basic_string<unsigned charin
the first place?

/Peter
Jun 27 '08 #4
On Apr 21, 12:32*pm, peter koch <peter.koch.lar...@gmail.comwrote:
On 21 Apr., 17:26, Hans Mull <hans72m...@aol.dewrote:
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 charor a std::basic_string<unsigned charin
the first place?
The usual answer to questions such as that is "legacy code."
Socks
Jun 27 '08 #5
On 2008-04-21 11:26:07 -0400, Hans Mull <ha********@aol.desaid:
How can I convert a string to a const unsigned char*?
(string::c_str() converts the string to a signed char)
Technically, c:str() returns const char*. A char, in turn, has the same
representation as either a signed char or an unsigned char, depending
on the compiler. But it's a different type from both.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by jagmeena | last post: by
2 posts views Thread by Andrew | last post: by
23 posts views Thread by YinTat | last post: by
9 posts views Thread by Gregory.A.Book | last post: by
11 posts views Thread by hamishd | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.