Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 826
BSchlinker Avatar asked Jul 08 '26 11:07

BSchlinker


1 Answers

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 :)

like image 159
Alex Budovski Avatar answered Jul 09 '26 23:07

Alex Budovski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!