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(st d::string&, size_t)'
candidates are: void MD5::update(uns igned char*, unsigned int)
void PrintMD5(string str){
MD5 context;
context.update( str, str.size());
context.finaliz e();
cout << "MD5: " << context << endl;
return;
}
I hope someone can help me solve this delema. 10 26678
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!!!
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.
<sp****@gmail.c omwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.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(st d::string&, size_t)'
candidates are: void MD5::update(uns igned char*, unsigned int)
void PrintMD5(string str){
MD5 context;
context.update( str, str.size());
context.finaliz e();
cout << "MD5: " << context << endl;
return;
}
I hope someone can help me solve this delema.
std::string.c_s tr() 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.
Jim Langston schrieb:
<sp****@gmail.c omwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.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(st d::string&, size_t)' candidates are: void MD5::update(uns igned char*, unsigned int)
void PrintMD5(string str){ MD5 context;
context.update( str, str.size()); context.finaliz e();
cout << "MD5: " << context << endl;
return; }
I hope someone can help me solve this delema.
std::string.c_s tr() 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<uns igned chartempV(str.b egin(), 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_naew e_AT_atlas_DOT_ de
"Stefan Naewe" <pl****@nospam. netwrote in message
news:6e******** ***@news01.atla s.de...
Jim Langston schrieb:
><sp****@gmail. comwrote in message news:11******* *************** @i3g2000cwc.goo glegroups.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(st d::string&, size_t)' candidates are: void MD5::update(uns igned char*, unsigned int)
void PrintMD5(string str){ MD5 context;
context.update( str, str.size()); context.finaliz e();
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<uns igned chartempV(str.b egin(), 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.
Jim Langston schrieb:
"Stefan Naewe" <pl****@nospam. netwrote in message
news:6e******** ***@news01.atla s.de...
>Jim Langston schrieb:
>><sp****@gmail .comwrote in message news:11****** *************** *@i3g2000cwc.go oglegroups.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(st d::string&, size_t)' candidates are: void MD5::update(uns igned char*, unsigned int)
void PrintMD5(string str){ MD5 context;
context.update( str, str.size()); context.finaliz e();
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.updat e( 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.updat e( TempString, strlen( TempString ) ); ...
Why not use a std::vector ?
std::vector<uns igned chartempV(str.b egin(), 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_naew e_AT_atlas_DOT_ de
"Jim Langston" <ta*******@rock etmail.comwrote in message
news:7E******** *****@newsfe02. lga...
std::string.c_s tr() 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
Im still reading the replys and will let you know what hapens soon.
Howard wrote:
"Jim Langston" <ta*******@rock etmail.comwrote in message
news:7E******** *****@newsfe02. lga...
std::string.c_s tr() 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
sp****@gmail.co m 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.c om/c++-faq-lite/how-to-post.html>
Brian This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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!";
int size1 = sizeof(std::string);
int size2 = sizeof(someString);
and printed out the values of size1 and size2. size1 and size2 always
|
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 'LPCWSTR'
I really get mad with that !!!
|
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 is quite importante - it's not a realtime
system, but almost - and I concern about std::string performance in
terms of speed. No doubt to use std implementation is a lot easier but
I can't sacrifice speed.
|
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 STL
How about allocating the vector ? should I know before the size ?
And I would like to do then opposite conversion, from an unsigned char
| |
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 ""
// 'words' are any sequences of non-whitespace characters
// leading, trailing and multiple whitespace characters
// should be ignored.
|
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 write to
the file:
error C2679: binary '<<' : no operator found which takes a right-hand
operand of type 'std::string' (or there is no acceptable conversion)
I don't know what I am doing wrong. I have posted my entire program
|
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 MultiByte String env (MBCS)
and wchar_t if UNICODE
#if defined(WIN32) || defined(UNDER_CE)
|
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";
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |