By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,804 Members | 1,516 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,804 IT Pros & Developers. It's quick & easy.

Converting a string to an const unsigned char*

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.