【问题标题】:C ascii to hex codeC ascii 到十六进制代码
【发布时间】: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


【解决方案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);
}

【讨论】:

    【解决方案2】:

    这是我一直在寻找的最终产品,但在解释/编码方面很差。

    #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;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      • 2015-08-23
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      相关资源
      最近更新 更多