简体   繁体   English

无法将 const char 转换为 char

[英]Cannot convert const char to char

char messaggiou[100];    
char *name2;
string encoded;

        name2 = (char*)malloc(encoded.length() + 1); 
        strcpy(name2, messaggiou);                                     
        strcpy(encoded.c_str(), name2);           
        const char* hex_str = name2;

I get an error with this code (simplified), with vs2015 in C ++.这段代码(简化版)出现错误,在 C++ 中使用 vs2015。

error C2664: `'char *strcpy(char *,const char *)': cannot convert argument 1 from 'const char *' to 'char *'

Ps In messaggiou there is an encrypted string in hex Ps 在 messaggiou 中有一个十六进制的加密字符串

if you use C++ I would do this in your place:如果你使用 C++,我会在你的地方这样做:

std::string messaggiou;    
std::string name2;
std::string encoded;

name2 = messaggiou;                                     
encoded = name2;          
const char* hex_str = name2.c_str();

Why complicate things if it can be easier, thanks to C++ :-)多亏了 C++ :-) 如果可以更容易,为什么要把事情复杂化呢?

You can also force any const with const_cast<char*>(const object) , but why if you don't need to...您还可以使用const_cast<char*>(const object)强制任何const ,但是如果您不需要...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM