c2664 cannot convert parameter 2 from 'std::string' to 'const void *' - c++

This is where I call the function..
memcpy(dataHashResult,sha1.operator()(dataBuffer,16),16);
I changed
/// compute SHA1 of a memory block
std::string operator()(const void* data, size_t numBytes);
to
std::string operator()(BYTE* data, size_t numBytes);
the types of buffers are..
unsigned char dataBuffer[64];
unsigned char dataHashResult[64];
Any help would be appreciated ..I'm new to C++..
Thanks.

try this:
memcpy(dataHashResult,sha1.operator()(dataBuffer,16).c_str(),16);
The problem is that memcpy needs a pointer to copy from. Your original code gives it a std::string object. Luckily, std::string has a c_str() function that returns the "C-style" string, i.e. a const char*.

Related

C++: How to convert 'const char*' to char

I know there are a lot of questions like this out there on StackOverflow, but I haven't been able to find any that help resolve my case. Whenever I try to do something like this:
// str = some string or char array
// some magic to get around fpermissive errors
stringstream convert;
convert << str;
// capture the stream's temporary string
const string store = convert.str();
// get a manageable array
const char* temp = store.c_str();
and then try to do something like atoi(temp[0]), I keep getting the classic conversion error that char couldn't be converted to const char. In the documentation for atoi and many other functions, const char is a required parameter. How can a char be sent in if there's only a const one? Does retrieving a char at a specific position auto-cast to char?
I'm not sure if this is what is causing the error, but atoi takes as its parameter not a char, but the pointer to one. So instead of
atoi(temp[0])
try this
atoi(&temp[0])
as that is a pointer.

Why doesn't reinterpret_cast convert 'unsigned char' to 'char'?

I am trying to compile this library using MSVC10, and this function is giving me headache:
/*! \brief Read bytes from a \c std::istream
\param is The stream to be read.
\param data A pointer to a location to store the bytes.
\param size The number of bytes to be read.
*/
void _read(std::istream &is, unsigned char *data, int size)
{
for (int i=0; i < size ; ++i )
is.get(static_cast<char>(data[i]));
}
error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::get(_Elem &)' : cannot convert parameter 1 from 'char' to 'char &'
The original used static_cast, so I try with reinterpret_cast as suggested elsewhere but that fails too:
error C2440: 'reinterpret_cast' : cannot convert from 'unsigned char' to 'char'
This library comes with unix makefiles. What is the best way to resolve this compile error?
Because reinterpret_cast does not work that way, by definition.
In order to perform memory re-interpretation, you have to apply reinterpret_cast to pointers or references. If you want to reinterpret unsigned char data as char data, you actually have to convert to char & type, not to char type.
In your case that would be
is.get(reinterpret_cast<char &>(data[i]));
Or you can go the pointer route and do
is.get(*reinterpret_cast<char *>(&data[i]));
(which is the same thing).
Because you need a char& that is a reference to char but the result of the cast is an r-value, and so not bindable to the reference.
You need something like:
is.get(reinterpret_cast<char&>(data[i]));
But in this particular case you can / should use static_cast<char&>:
is.get(static_cast<char&>(data[i]));
Try this instead:
void _read(std::istream &is, unsigned char *data, int size)
{
for (int i=0; i < size ; ++i )
is.get(reinterpret_cast<char*>(data)[i]);
}
In addition to the other answers which cope with the casting problem:
Why don't you just use istream::read to read size bytes at once? This avoids your hand-crafted for-loop and should be faster.
void _read(std::istream &is, unsigned char *data, int size) {
is.read(reinterpret_cast<char*>(data), size);
}

Deprecated conversion from string constant to char * error [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ deprecated conversion from string constant to ‘char*’
I am having following code, though i didn't copy full code because it is huge.
Following code is in template class, and i am getting warning as below. Because of warning in template i am not able to instantiate it and getting "instantiated from here" error.
warning: deprecated conversion from string constant to 'char*''
void ErrorMessageInRaphsodyCode(char* pcCompleteMessage, char* pcMessage, char* pcFileName, unsigned int RowNo)
{
//...
}
char cCompleteMessage[200];
memset(cCompleteMessage, 0x00, sizeof(cCompleteMessage));
char*cMessage = "add reorgenize failed";
ErrorMessageInRaphsodyCode(cCompleteMessage, cMessage, "omcollec.h", __LINE__);
My question is what is best way to get rid of above warning ?
If a function takes a char const *, it guarantees that it only reads whatever data the pointer points to. However, if it takes a non-const pointer, like char *, it might write to it.
As it is not legal to write to a string literal, the compiler will issue a warning.
The best solution is to change the function to accept char const * rather than char *.
char cMessage[] = "add reorganize failed";
This should get rid of the warning.
Best way to get rid of it is to fix the function that is taking the parameter.
If your code is correct and the function does indeed take string constants, it should say so in its prototype:
void ErrorMessageInRaphsodyCode(char* pcCompleteMessage, char* pcMessage, const char* pcFileName, unsigned int RowNo)
If you can't do that (you don't have the code), you can create an inline wrapper:
inline void ErrorMessageInRaphsodyCodeX(char* p1, char* p2, const char* p3, unsigned int p4)
{ ErrorMessageInRaphsodyCode(p1,p2,(char*)p3,p4); }
and use the wrapper instead.
If your code is incorrect and the function does actually require writeable memory (which I highly doubt), you will need to make the string writeable by either creating a local array as Jan suggested, or mallocating enough memory.
(1) Make the variable a const char*
(..., const char* pcFileName, ...)
(2) If above is not possible and you want to retain the state of char* and const char* then make the function a template:
template<typename CHAR_TYPE> // <--- accepts 'char*' or 'const char*'
void ErrorMessageInRaphsodyCode(char* pcCompleteMessage, CHAR_TYPE* pcMessage, char* pcFileName, unsigned int RowNo)
{
//...
}
function c_str() of std::string class.

How to initialise std::istringstream from const unsigned char* without cast or copy?

I have binary data in a byte sequence described by const unsigned char *p and size_t len. I want to be able to pass this data to a function that expects a std::istream *.
I think I should be able to do this without copying the data, unsafe casts or writing a new stream class. But so far I'm failing. Can anyone help?
Update
Thanks all for the comments. This would seem to be an unanswerable question because std::istream operates with char and conversion would at some point require at least an integer cast from unsigned char.
The pragmatic approach is to do this:
std::string s(reinterpret_cast<const char*>(p), len);
std::istringstream i(s);
and pass &i to the function expecting std::istream *.
Your answer is still copying.
Have you considered something like this?
const unsigned char *p;
size_t len;
std::istringstream str;
str.rdbuf()->pubsetbuf(
reinterpret_cast<char*>(const_cast<unsigned char*>(p)), len);

Const unsigned char* to char*

So, I have two types at the moment:
const unsigned char* unencrypted_data_char;
string unencrypted_data;
I'm attempting to perform a simple conversion of data from one to the other (string -> const unsigned char*)
As a result, I have the following:
strcpy((unencrypted_data_char),(unencrypted_data.c_str()));
However, I'm receiving the error:
error C2664: 'strcpy' : cannot convert parameter 1 from 'const unsigned char *' to 'char *'
Any advice? I thought using reinterpret_cast would help, but it doesn't seem to make a difference.
You can't write to a const char *, because each char pointed to is const.
strcpy writes to the first argument. Hence the error.
Don't make unencrypted_data_char const, if you plan on writing to it (and make sure you've allocated enough space for it!)
And beware of strcpy's limitations. Make sure you know how big your buffer needs to be in advance, because strcpy doesn't stop 'til it gets enough :)
well if unencrypted_data_char point to a memory that is only readable,you'd better not to write any data on it,it will certainly cause a segment fault.
e.g:
const char *a="abc";
a pointed to a readable only memory
if unencrypted_data_char is const only because you let it be(like const char* a=b),well you could use const_cast< char* >(a) to conver it.
if converting from const char* to unsigned char*.
1.you need convert from const char* to char*.use const_cast.
2.conver from char* to unsigned char*. use reinterpret_cast.