从std :: string转换为unsigned char * [英] Convert from std::string to unsigned char*
问题描述
我是一个新手,但也许某人可以帮助我。我一直在寻找一种方法将std :: string转换为unsigned char *
情况是我有一个想要一个unsigned char的函数*和我
想给它一个std :: string
没有匹配函数来调用`MD5 :: update(std :: string&, size_t)''
候选者是:void MD5 :: update(unsigned char *,unsigned int)
void PrintMD5(string str){
MD5上下文;
context.update(str,str.size());
context.finalize();
cout<< MD5: << context<<结束;
返回;
}
我希望有人可以帮我解决这个问题。
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.
推荐答案
sposes写道:
sposes wrote:
context.update(str,str.size ());
context.update(str, str.size());
context.update(str.c_str(),str.size());
请阅读一大块在恢复编码之前的C ++教程。它可能
没有覆盖c_str(),但你仍然会选择足够的提示来为自己计算
c_str()!
-
Phlip
http ://c2.com/cgi/wiki?ZeekLand < - 不是博客!!!
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写道:
Phlip wrote:
sposes写道:
sposes wrote:
> context.update(str,str.size());
> context.update(str, str.size());
context.update(str.c_str(),str.size());
context.update(str.c_str(), str.size());
那不行,str.c_str()返回const char *,OP的函数
需要unsigned char *。
传递std :: string可能不安全,考虑到函数没有
将const指针作为参数。
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.
在恢复编码之前,请阅读C ++教程的一大部分内容。它可能
没有覆盖c_str(),但你仍然会选择足够的技巧来为自己计算
c_str()!
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!
无评论....
-
Ian Collins。
No comment....
--
Ian Collins.
>
< sp **** @ gmail.comwrote in message
news:11 ********************** @ i3g2000cwc.googlegro ups.com ...
<sp****@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
我非常喜欢新手,但也许某人可以帮助我。我一直在寻找一种方法将std :: string转换为unsigned char *
情况是我有一个想要一个unsigned char的函数*和我
想给它一个std :: string
没有匹配函数来调用`MD5 :: update(std :: string&, size_t)''
候选者是:void MD5 :: update(unsigned char *,unsigned int)
void PrintMD5(string str){
MD5上下文;
context.update(str,str.size());
context.finalize();
cout<< MD5: << context<<结束;
返回;
}
我希望有人可以帮我解决这个问题。
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()将返回一个const char *。请注意这个
是const。这意味着内容不能通过这个来改变。
如果
context.update(unsigned char *,unsigned int)
将改变char *的内容,我非常期待它
将,你不能使用它。
我在这些情况下的解决方案是将c_str()实际复制到一个char数组
将其复制回来。
unsigned char * TempString = new unsigned char [str.size()* 2];
strcpy(TempString,str.c_str());
context.update(TempString,strlen(TempString));
....
如果您知道更新将不会更改内容的全部事实
字符串,你必须绝对确定这一点,你可以抛弃
const。
context.update(const_cast< char *>(str .c_str()),str.size());
我为一个我使用的设计师不使用const的库做这个
correc tness。
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.
这篇关于从std :: string转换为unsigned char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!