473,288 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

Convert from std::string to unsigned char*

Im very much a newbie but perhaps somehone can help me. Ive been
searching for a way to convert a std::string to a unsigned char*

The situation is I have a function that wants a unsigned char* and I
want to give it a std::string

no matching function for call to `MD5::update(std::string&, size_t)'
candidates are: void MD5::update(unsigned char*, unsigned int)

void PrintMD5(string str){
MD5 context;

context.update(str, str.size());
context.finalize();

cout << "MD5: " << context << endl;

return;
}

I hope someone can help me solve this delema.

Aug 21 '06 #1
10 26485
sposes wrote:
context.update(str, str.size());
context.update(str.c_str(), str.size());

Please read a good chunk of a C++ tutorial before resuming coding. It might
not have covered c_str(), yet you would still pick enough tips to figure the
c_str() out for yourself!

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Aug 21 '06 #2
Phlip wrote:
sposes wrote:

> context.update(str, str.size());


context.update(str.c_str(), str.size());
That won't work, str.c_str() returns const char*, the OP's function
requires unsigned char*.

Passing a std::string might be unsafe, considering the function doesn't
take a const pointer as a parameter.
Please read a good chunk of a C++ tutorial before resuming coding. It might
not have covered c_str(), yet you would still pick enough tips to figure the
c_str() out for yourself!
No comment....

--
Ian Collins.
Aug 21 '06 #3
<sp****@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Im very much a newbie but perhaps somehone can help me. Ive been
searching for a way to convert a std::string to a unsigned char*

The situation is I have a function that wants a unsigned char* and I
want to give it a std::string

no matching function for call to `MD5::update(std::string&, size_t)'
candidates are: void MD5::update(unsigned char*, unsigned int)

void PrintMD5(string str){
MD5 context;

context.update(str, str.size());
context.finalize();

cout << "MD5: " << context << endl;

return;
}

I hope someone can help me solve this delema.
std::string.c_str() will return a const char *. Notice very carefully this
is const. That means that the contents can't be changed through this.

If the
context.update( unsigned char*, unsigned int )
is going to change the contents of the char *, which I highly expect it
will, you can't use this.

My solution in these cases is to actually copy the c_str() to a char array
to copy it back.

unsigned char* TempString = new unsigned char[ str.size() * 2 ];
strcpy( TempString, str.c_str() );
context.update( TempString, strlen( TempString ) );
....

If you know for a total fact that update will not be changing the contents
of the string, and you have to be absolutely sure of this, you can cast away
the const.

context.update( const_cast< char* >( str.c_str() ), str.size() );

I do this for one library I use where the designer is not using const
correctness.
Aug 21 '06 #4
Jim Langston schrieb:
<sp****@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
>Im very much a newbie but perhaps somehone can help me. Ive been
searching for a way to convert a std::string to a unsigned char*

The situation is I have a function that wants a unsigned char* and I
want to give it a std::string

no matching function for call to `MD5::update(std::string&, size_t)'
candidates are: void MD5::update(unsigned char*, unsigned int)

void PrintMD5(string str){
MD5 context;

context.update(str, str.size());
context.finalize();

cout << "MD5: " << context << endl;

return;
}

I hope someone can help me solve this delema.

std::string.c_str() will return a const char *. Notice very carefully this
is const. That means that the contents can't be changed through this.

If the
context.update( unsigned char*, unsigned int )
is going to change the contents of the char *, which I highly expect it
will, you can't use this.

My solution in these cases is to actually copy the c_str() to a char array
to copy it back.

unsigned char* TempString = new unsigned char[ str.size() * 2 ];
Why 'size * 2' ??
strcpy( TempString, str.c_str() );
You'd need a cast here.
context.update( TempString, strlen( TempString ) );
...
Why not use a std::vector ?

std::vector<unsigned chartempV(str.begin(), str.end());
context.update(&tempV[0], tempV.size());

If you know for a total fact that update will not be changing the contents
of the string, and you have to be absolutely sure of this, you can cast away
the const.

context.update( const_cast< char* >( str.c_str() ), str.size() );

I do this for one library I use where the designer is not using const
correctness.
/S
--
Stefan Naewe
stefan_DOT_naewe_AT_atlas_DOT_de
Aug 21 '06 #5

"Stefan Naewe" <pl****@nospam.netwrote in message
news:6e***********@news01.atlas.de...
Jim Langston schrieb:
><sp****@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegr oups.com...
>>Im very much a newbie but perhaps somehone can help me. Ive been
searching for a way to convert a std::string to a unsigned char*

The situation is I have a function that wants a unsigned char* and I
want to give it a std::string

no matching function for call to `MD5::update(std::string&, size_t)'
candidates are: void MD5::update(unsigned char*, unsigned int)

void PrintMD5(string str){
MD5 context;

context.update(str, str.size());
context.finalize();

cout << "MD5: " << context << endl;

return;
}

I hope someone can help me solve this delema.

std::string.c_str() will return a const char *. Notice very carefully
this
is const. That means that the contents can't be changed through this.

If the
context.update( unsigned char*, unsigned int )
is going to change the contents of the char *, which I highly expect it
will, you can't use this.

My solution in these cases is to actually copy the c_str() to a char
array
to copy it back.

unsigned char* TempString = new unsigned char[ str.size() * 2 ];

Why 'size * 2' ??
It is unknown how much the update function is going to change the string.
*2 was a guess at worst case scenario. It may need to be *100, or simply a
fixed limit, unknown without knowing what update is going to do.
>strcpy( TempString, str.c_str() );

You'd need a cast here.
Hmm.. definition I have for strcpy is:
char *strcpy( char *strDestination, const char *strSource );

The only thing I see different is TempString is unsigned where
strDestination is signed. Won't it only give a warning for that?
>context.update( TempString, strlen( TempString ) );
...

Why not use a std::vector ?

std::vector<unsigned chartempV(str.begin(), str.end());
context.update(&tempV[0], tempV.size());
Well, it is presumed that update will change the content (otherwise they can
just use the const_cast) so it's also presumed that the size of the string
will be different. As such, you haven't allocated enough space in your
vector so are going to need another call to allocate more space. Since the
main purpose of using std::vector and std::string over char arrays is
dynamic growth, and because we don't have dynamic growth in the temp object,
might as well just use the char array since we have to allocate the size
anyway. Using std::vector gains us nothing, unless the size is fixed.
>If you know for a total fact that update will not be changing the
contents
of the string, and you have to be absolutely sure of this, you can cast
away
the const.

context.update( const_cast< char* >( str.c_str() ), str.size() );

I do this for one library I use where the designer is not using const
correctness.

Aug 21 '06 #6
Jim Langston schrieb:
"Stefan Naewe" <pl****@nospam.netwrote in message
news:6e***********@news01.atlas.de...
>Jim Langston schrieb:
>><sp****@gmail.comwrote in message
news:11**********************@i3g2000cwc.googleg roups.com...
Im very much a newbie but perhaps somehone can help me. Ive been
searching for a way to convert a std::string to a unsigned char*

The situation is I have a function that wants a unsigned char* and I
want to give it a std::string

no matching function for call to `MD5::update(std::string&, size_t)'
candidates are: void MD5::update(unsigned char*, unsigned int)

void PrintMD5(string str){
MD5 context;

context.update(str, str.size());
context.finalize();

cout << "MD5: " << context << endl;

return;
}

I hope someone can help me solve this delema.
std::string.c_str() will return a const char *. Notice very carefully
this
is const. That means that the contents can't be changed through this.

If the
context.update( unsigned char*, unsigned int )
is going to change the contents of the char *, which I highly expect it
will, you can't use this.

My solution in these cases is to actually copy the c_str() to a char
array
to copy it back.

unsigned char* TempString = new unsigned char[ str.size() * 2 ];
Why 'size * 2' ??

It is unknown how much the update function is going to change the string.
*2 was a guess at worst case scenario. It may need to be *100, or simply a
fixed limit, unknown without knowing what update is going to do.
True.
Who knows without seeing the doc for MD5::update()
>>strcpy( TempString, str.c_str() );
You'd need a cast here.

Hmm.. definition I have for strcpy is:
char *strcpy( char *strDestination, const char *strSource );

The only thing I see different is TempString is unsigned where
strDestination is signed. Won't it only give a warning for that?
My g++ gives me:

"error: invalid conversion from 'unsigned char*' to 'char*' "
>>context.update( TempString, strlen( TempString ) );
...
Why not use a std::vector ?

std::vector<unsigned chartempV(str.begin(), str.end());
context.update(&tempV[0], tempV.size());

Well, it is presumed that update will change the content (otherwise they can
just use the const_cast) so it's also presumed that the size of the string
will be different. As such, you haven't allocated enough space in your
vector so are going to need another call to allocate more space. Since the
main purpose of using std::vector and std::string over char arrays is
dynamic growth, and because we don't have dynamic growth in the temp object,
might as well just use the char array since we have to allocate the size
anyway. Using std::vector gains us nothing, unless the size is fixed.
Even if MD5::update() changed the passed string using, a std::vector would
IMHO be better because:
- no way to forget to delete[]
- no need to cast
- no bad feeling in the neck because std::strings' memory is not guaranteed
to be contiguous (at least not yet IIRC)

/S
--
Stefan Naewe
stefan_DOT_naewe_AT_atlas_DOT_de
Aug 21 '06 #7

"Jim Langston" <ta*******@rocketmail.comwrote in message
news:7E*************@newsfe02.lga...
std::string.c_str() will return a const char *. Notice very carefully
this is const. That means that the contents can't be changed through
this.

If the
context.update( unsigned char*, unsigned int )
is going to change the contents of the char *, which I highly expect it
will, you can't use this.

My solution in these cases is to actually copy the c_str() to a char array
to copy it back.

unsigned char* TempString = new unsigned char[ str.size() * 2 ];
strcpy( TempString, str.c_str() );
context.update( TempString, strlen( TempString ) );
...
It's a bad idea to simply "guess" what a function will do with a string, and
how long an array you might need. Such guesses are bound to either 1) fail
miserably at some point, or 2) use up memory needlessly. And nobody's going
to understand why you've chosen to arbitrarily double the size.

I doubt that update() is updating the string. More likely, it's updating
the "context" object. If it _is_ updating the string, then it should be
doing the allocation as well. It's poorly designed if you tell it how much
data it _can_ use, but it decides how much it _needs_ to use. What should
it do if it needs more than you gave it?

-Howard

Aug 21 '06 #8
Im still reading the replys and will let you know what hapens soon.
Howard wrote:
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:7E*************@newsfe02.lga...
std::string.c_str() will return a const char *. Notice very carefully
this is const. That means that the contents can't be changed through
this.

If the
context.update( unsigned char*, unsigned int )
is going to change the contents of the char *, which I highly expect it
will, you can't use this.

My solution in these cases is to actually copy the c_str() to a char array
to copy it back.

unsigned char* TempString = new unsigned char[ str.size() * 2 ];
strcpy( TempString, str.c_str() );
context.update( TempString, strlen( TempString ) );
...

It's a bad idea to simply "guess" what a function will do with a string, and
how long an array you might need. Such guesses are bound to either 1) fail
miserably at some point, or 2) use up memory needlessly. And nobody's going
to understand why you've chosen to arbitrarily double the size.

I doubt that update() is updating the string. More likely, it's updating
the "context" object. If it _is_ updating the string, then it should be
doing the allocation as well. It's poorly designed if you tell it how much
data it _can_ use, but it decides how much it _needs_ to use. What should
it do if it needs more than you gave it?

-Howard
Aug 21 '06 #9
sp****@gmail.com wrote:
Im still reading the replys and will let you know what hapens soon.

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>

Brian

Aug 21 '06 #10

"Howard" <al*****@hotmail.comwrote in message
news:kV********************@bgtnsc04-news.ops.worldnet.att.net...
>
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:7E*************@newsfe02.lga...
>std::string.c_str() will return a const char *. Notice very carefully
this is const. That means that the contents can't be changed through
this.

If the
context.update( unsigned char*, unsigned int )
is going to change the contents of the char *, which I highly expect it
will, you can't use this.

My solution in these cases is to actually copy the c_str() to a char
array to copy it back.

unsigned char* TempString = new unsigned char[ str.size() * 2 ];
strcpy( TempString, str.c_str() );
context.update( TempString, strlen( TempString ) );
...

It's a bad idea to simply "guess" what a function will do with a string,
and how long an array you might need. Such guesses are bound to either 1)
fail miserably at some point, or 2) use up memory needlessly. And
nobody's going to understand why you've chosen to arbitrarily double the
size.
I agree. I had planned on explaining why I was using str.size() * 2 instead
of simply str.size() but had forgot. My bad.
I doubt that update() is updating the string. More likely, it's updating
the "context" object. If it _is_ updating the string, then it should be
doing the allocation as well. It's poorly designed if you tell it how
much data it _can_ use, but it decides how much it _needs_ to use. What
should it do if it needs more than you gave it?
I doubt it highly too. Most likely the method is just not const
correctness, and if the OP has the source to the class he should fix it to
be const unsigned char*.

I use a library where I don't have the source (it's a .dll) but only the
interface so can't mess with the signatures by fixing the const correctness
myself and in those cases I use a const_cast because I know for a fact that
the function is not going to change the string.

Aug 21 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
12
by: jl_post | last post by:
Dear C++ community, I have a question regarding the size of C++ std::strings. Basically, I compiled the following code under two different compilers: std::string someString = "Hello, world!";...
8
by: ppcdev | last post by:
Here's what I try : LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::StringToHGlobalUni(str); c:\MyNetPrj\Prj0001\stunt.cpp(244): error C2440: 'type cast' : cannot convert from 'System::IntPtr' to...
37
by: jortizclaver | last post by:
Hi, I'm about to develop a new framework for my corporative applications and my first decision point is what kind of strings to use: std::string or classical C char*. Performance in my system...
3
by: timor.super | last post by:
Hi group, how to convert a string to a vector of unsigned char ? I used to iterate trough the string to set the vector, but I think this is not the best way to do this. I'm a beginner with the...
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
29
by: aarthi28 | last post by:
Hi, I have written this code, and at the end, I am trying to write a vector of strings into a text file. However, my program is nor compiling, and it gives me the following error when I try to...
14
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
4
by: Man4ish | last post by:
HI , I am trying to convert string into char array of characters.but facing problem. #include <iostream> #include <string> using namespace std; int main() { string t="1,2,3,4,5,6";
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.