1、功能要求
将一个不定长的字符串转换为一个定长的数字
输入:字符串
输出:16位数字
转换格式:将字符串分成n组,每组16个字符
将n组字符串相应位置的字符相加,如果值不是个数,则各个位再进行相加
直到为个位数为止,最终得出的16个数字即要求输出的数字
2、程序文件
#include <stdio.h>
#include <string.h>
int main()
{
char buf[100];
int res[16] = {0};
printf ("请输入字符串:");
scanf ("%s", buf);
int len = strlen(buf);
int i;
for (i = 0; i < 16; i++)
{
int tmp = 0;
while (tmp+i < len)
{
//res[0] = resuf[0] + resuf[16] + resuf[32]......
res[i] += buf[tmp+i];
tmp += 16;
}
while (res[i] > 9)
{
res[i] = res[i]%10 + res[i]/10;
}
}
for (i = 0; i < 16; i++)
{
printf ("%d", res[i]);
}
printf ("\n");
return 0;
}
3、测试结果
root@lj:/字符加密# ./a.out
请输入字符串:abckjabf12q834y4
7898778345526747