带有无符号字符字符串的getline
getline with unsigned char string
所以,我搜索了一下为什么我的编译器会给出一个错误:
49 ~C++SHA-1main.cpp invalid conversion from `unsigned char*' to `char*'
我发现你不能在无符号字符和字符之间转换,因为它们是完全不同的类型。因此,这导致我在代码中需要一个getline函数和一个无符号的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:nnt1. Text Stringnt2. Hex Stringnt3. Binary Stringn";
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 hashednn";
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;
}
如果有人能给我一些建议,那将是有帮助的。
感谢
这与严格的别名约束有关:不允许通过字符指针引用无符号字符数组。
#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*
。
调用getline
:时将数组强制转换为(char *)
getline((char *)input, ...);
相关文章:
- 如何在 c++ 中使用 getline 从文件中读取字符串?
- 确切地说,如何解释 std::getline(stream, string) 函数在C++中填充的字符串
- 而循环跳过 cin.getline() 对于 C 字符串
- 使用 getline(cin, var) 两次在进行字符串比较时会产生错误 (==)
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较
- 比较字符串的 GetLine 工作一次,然后比较之后不起作用
- 字符串和getline字符
- 如何对字符串::getline进行长度限制
- 当 getline 用于获取整个字符串时,程序崩溃
- 使用getline和字符串函数C++程序中的分段错误
- 将 int 与字符串和 getline 一起使用,没有错误
- 无法使用 getline() 输入字符串.它只读取一个字符串
- 如何使此函数使用 getline 读取字符串并使用 int 执行相同的行为?
- std::getline 读取最后一个字符串两次
- 使用 getline 读取字符串,但每行中都有换行符
- 在getline()之后获取字符串
- 使用C 中字符串标头的getline()函数将第一个char存储在字符串对象中
- ifstream gcount在getline字符串重载时返回0
- 什么是我不了解getline+字符串
- 在getline字符串中获取字符串