【发布时间】:2015-03-21 18:14:59
【问题描述】:
我需要将 ascii 输入转换为十六进制输入。我对 C 很不好,所以如果你能包含一些非常有帮助的解释。这段代码只是一堆零碎的代码,但大多数可能是错误的或无用的。之后我需要使用用户输入来选择字符串,但最难的部分是让它完全转换。
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
void crypt(char *buf, char *keybuf, int keylen) {
//This is meant to encrypt by xor-ing with the sentence and key entered//
//It is also supposed to replace the original buf with the new version post-xor//
int i;
int *xp;
xp=&i;
for(i=0; i<keylen; i++) {
buf[i]=buf[i]^keybuf[i];
xp++;
}
}
int convertkey(char *keybuf) {
int keylen=0;
//I need to add something that will return the length of the key by incrementing keylen according to *keybuf//
return keylen;
}
int main(int argc, char * argv[]){
char x;
char *xp;
xp = &x;
char a[47];
char *ap;
ap=a;
printf("Enter Sentence: ");
scanf("%[^\n]",a);
printf("Enter key: ");
scanf("%d",xp);
printf("You entered the sentence: %s\n",a);
printf("You entered the key: %d\n",x);
convertkey(xp);
crypt(ap,xp,x);
printf("New Sentence: %s\n",a);
return 0;
}
【问题讨论】:
-
C 不支持嵌套函数,首先。将 'crypt' 和 'convertkey' 移到 main() 之外。
-
可悲的是,“大多数可能是错误的或无用的”是一个公平的评价。我认为如果不根据您的要求实际重写代码就无法改进您的代码,因为无论如何他们都不清楚,即使有人想要这样做也会很困难,他们可能不会这样做。
-
“将 ASCII 输入转换为十六进制输入”。举一个或 2 或 3 个例子有助于阐明目标。
标签: c encryption type-conversion hex ascii