我需要将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;
}
答案 0 :(得分:1)
就是这样,我已经重新组织了你发布的代码,所以至少它会编译,即使意图不明确。也许你可以从这里接受它。
#include <stdio.h>
#include <stdlib.h>
// moved out of main()
void crypt(char *buf, char *keybuf, int keylen) {
int i; // added declaration
for(i=0; i<keylen; i++) { // corrected syntax and end condition
buf[i]=buf[i]^keybuf[i];
//xp++; // out of scope
}
}
// moved out of main()
int convertkey(char *keybuf) {
int keylen=0;
return keylen;
}
int main(int argc, char * argv[]){
int x=0;
int *xp;
xp = &x; // xp=&x{0};
return 0; // exit(0);
}
答案 1 :(得分:0)
这是我正在寻找的最终产品,但在解释/编码时非常差。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void crypt(char *buf, char *keybuf, int keylen) {
int i;
int length= strlen(buf)-1;
for(i=0; i<length; i++) {
buf[i]=buf[i]^keybuf[i%keylen];
printf("%c",buf[i]);
}
printf("\n");
}
int convertkey(char *keybuf) {
int i=0;
for(i=0;keybuf[i]!='\n';i++){
if(keybuf[i]>='0' & keybuf[i]<='9'){
keybuf[i]=keybuf[i]-'0';
}
else if(keybuf[i]>='a' & keybuf[i]<='f'){
keybuf[i]=(keybuf[i]-'a')+10;
}
}
return i;
}
int main(int argc, char * argv[]){
char keychars[12];
char a[48];
char *ap;
int i;
ap=a;
printf("Enter Sentence: ");
fgets(a, 48, stdin);
printf("Enter Key: ");
fgets(keychars, 12, stdin);
for (i=0; i<strlen(keychars); i++) {
char c = keychars[i];
printf("keychars[%d]=%c (character), %d (decimal), %x (hex)\n", i, c, c, c);
}
crypt(ap,keychars,convertkey(keychars));
return 0;
}