2014-04-29 50 views
0

所以,我做了,为什么我的编译器会发出一个错误说一些搜索:函数getline与无符号的字符字符串

49 ~\C++\SHA-1\main.cpp invalid conversion from `unsigned char*' to `char*' 

而且我发现,你不能无符号的字符之间的转换成char,因为它们是完全不同的类型。所以这导致了我需要在我的代码中使用getline函数和unsigned char字符串的问题。

#include <iostream> 
#include <stdint.h> 

using namespace std; 

uint32_t rotl(uint32_t value, int shift) 
{ 
     if ((shift &= sizeof(value)*8 - 1) == 0) return value; 
     return (value << shift) | (value >> (sizeof(value)*8 - shift)); 
} 

uint32_t rotr(uint32_t value, int shift) 
{ 
     if ((shift &= sizeof(value)*8 - 1) == 0) return value; 
     return (value >> shift) | (value << (sizeof(value)*8 - shift)); 
} 

int textInput(); 
int hexInput(); 
int binInput(); 

unsigned char message[64]; 

int SHA_1(); 

int main() 
{ 
    int selection; 
    cout<<"Select Input type:\n\n\t1. Text String\n\t2. Hex String\n\t3. Binary String\n"; 
    cin>>selection; 
    cin.ignore(); 
    switch(selection) 
    { 
        case 1: textInput(); break; 
        case 2: hexInput(); break; 
        case 3: binInput(); break; 
    } 
    SHA_1(); 
    cout<<"\ndone"; 
    cin.get(); 

    return 0; 
} 

int textInput() 
{ 
    unsigned char input[63] = {0}; 
    cout<<"Enter a text string to be hashed\n\n"; 
    cin.getline(input, 62, '\n'); 
    cin.ignore(); 
    for(int x = 0; x <= 63; x++) 
      { 
       //cout<<x<<"\n"; 
       if (input[x] == 0x00) 
       { 
           message[x] = 0x00000080; 
           message[63] = x; //This might be wrong. 
           //cout<<std::hex<<message; 
           break; 
       } 
       else message[x] = input[x]; 

      } 

    return 0; 
} 

int hexInput() 
{ 
    return 0; 
} 

int binInput() 
{ 
    return 0; 
} 

int SHA_1() 
{ 
    uint32_t h0 = 0x67452301; 
    uint32_t h1 = 0xEFCDAB89; 
    uint32_t h2 = 0x98BADCFE; 
    uint32_t h3 = 0x10325476; 
    uint32_t h4 = 0xC3D2E1F0; 

    uint32_t a; 
    uint32_t b; 
    uint32_t c; 
    uint32_t d; 
    uint32_t e; 

    uint32_t f; 
    uint32_t k; 

    uint32_t temp; 

    uint32_t w[80]; 

    /*for(int m = 0; m <= 63; m++) 
    { 
     cout<<"message["<<m<<"]="<<std::hex<<int(message[m])<<std::dec<<"\n"; 
    }*/ 

    for(int i = 0; i <= 15; i++) 
    { 
     w[i] = ((message[(i*4)] << 24) | (message[(i*4) + 1] << 16) | (message[(i*4) + 2] << 8) | (message[(i*4) + 3])); 
     //cout<<"W["<<i<<"]="<<std::hex<<w[i]<<std::dec<<"\n"; 
    } 

    for(int i = 16; i <= 79; i++) 
    { 
     w[i] = rotl((w[i - 3]^w[i - 8]^w[i - 14]^w[i - 16]), 1); 
    } 

    a = h0; 
    b = h1; 
    c = h2; 
    d = h3; 
    e = h4; 

    for(int iteration = 0; iteration <= 79; iteration++) 
    { 
      if((0 <= iteration) && (iteration <= 19)) 
      { 
        f = ((b & c) | ((~b) & d)); 
        k = 0x5A827999; 
      } 
      else if((20 <= iteration) && (iteration <= 39)) 
      { 
        f = (b^c^d); 
        k = 0x6ED9EBA1; 
      } 
      else if((40 <= iteration) && (iteration <= 59)) 
      { 
        f = ((b & c) | (b & d) | (c & d)); 
        k = 0x8F1BBCDC; 
      } 
      else if((60 <= iteration) && (iteration <= 79)) 
      { 
        f = (b^c^d); 
        k = 0xCA62C1D6; 
      } 

      temp = (rotl(a, 5) + f + e + k + w[iteration]); 
      e = d; 
      d = c; 
      c = rotl(b, 30); 
      b = a; 
      a = temp; 
    } 

    h0 = h0 + a; 
    h1 = h1 + b; 
    h2 = h2 + c; 
    h3 = h3 + d; 
    h4 = h4 + e; 

    cout<<hex<<h0<<" "<<h1<<" "<<h2<<" "<<h3<<" "<<h4; 


    return 0; 
} 

如果有人可以给我一些有用的建议。

感谢

+0

你为什么使用无符号字符?为什么你不使用'std :: string'? –

+0

因为我学会了使用字符数组。在char []上使用std :: string是否有优势? –

+0

是的。您的输入不限于64个字符。例如,你可以做:'字符串输入; cout <<“输入要散列的文本字符串”<< endl; getline(cin,input);' –

回答

0

这与strict-aliasing限制:你不得通过字符指针引用一个unsigned char数组。

#include <iostream> 
using namespace std; 

void aliasing_fun(char* arr) { 
    arr[0] = 42; 
} 

int main() { 
    unsigned char arr[10] = {0}; 
    aliasing_fun(arr); // Not allowed 

    return 0; 
} 

http://ideone.com/r4OVZi

你可能会投你的阵列char*才能做到这一点。

0

投你的阵列(char *)调用getline时:

getline((char *)input, ...); 
+0

我喜欢zmbq和Marco的答案。一个人提供了如何直接用一个例子来做到这一点,另一个则进一步细化。如果可以的话,我会把它们都标记为答案。 –