无效转换const unsigned char

时间:2016-03-31 20:44:54

标签: c++

我的程序中有这个功能:

unsigned char * stringXOR(const unsigned char * X, const unsigned char * Y, int len)
{
    unsigned char * result = new unsigned char [1024];
    for(int i = 0; i < len; i++)
    {
        result[i] = X[i] ^ Y[i];
    }
    result[len] = '\0';
    return result;
}

在main()中:

unsigned char ot1[1024] = "ksjdhsjd";
unsigned char ot2[1024] = "jjjkjhyh";

unsigned char * computeKey;
computeKey = stringXOR(ot1, ot2, strlen(ot1));

我收到错误invalid conversion from ‘unsigned char*’ to ‘const char*’ [-fpermissive]|

这里有什么问题?我没有回复任何常数,所以抱怨什么?

2 个答案:

答案 0 :(得分:2)

strlen期待const char *。您正试图将const unsigned char *传递给它。这就是错误消息的内容。

答案 1 :(得分:2)

错误与转换到const无关,而是将指针转换为unsigned char指向(const)char *的指针。