C ascii转十六进制代码
C ascii to hex code
我需要将 ascii 输入转换为十六进制输入。我对 C 非常不满意,所以如果您能提供一些非常有帮助的解释。这段代码只是一堆零碎的代码,但大多数可能是错误的或无用的。之后我需要使用用户输入 select 字符串,但困难的部分是让它完全转换。
#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;
}
事实上,我已经重新组织了您发布的代码,所以至少它可以编译,即使意图不明确。也许你可以从这里开始。
#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);
}
这是我一直在寻找的最终产品,但在 explaining/coding 方面表现很差。
#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;
}
我需要将 ascii 输入转换为十六进制输入。我对 C 非常不满意,所以如果您能提供一些非常有帮助的解释。这段代码只是一堆零碎的代码,但大多数可能是错误的或无用的。之后我需要使用用户输入 select 字符串,但困难的部分是让它完全转换。
#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;
}
事实上,我已经重新组织了您发布的代码,所以至少它可以编译,即使意图不明确。也许你可以从这里开始。
#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);
}
这是我一直在寻找的最终产品,但在 explaining/coding 方面表现很差。
#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;
}