【问题标题】:converting binary to corresponding ASCII character将二进制转换为相应的 ASCII 字符
【发布时间】:2020-02-03 08:03:59
【问题描述】:

所以我正在尝试创建一个程序,其中用户输入一个 12 位二进制汉明码序列,如“100010010001”,它应该打印出其对应的 ASCII 字符,在本例中为“A”。

我试图让我的程序忽略位于 _ _ 0 _ 1 0 0 _ 0 0 0 1 中的 4 个奇偶校验位,并将其他 8 个位移过来,以便它们在一起。在 else 语句中,我尝试将剩余的 8 位转换为一个字符。但是,当我尝试运行该程序时,在我键入二进制序列并按 Enter 后程序崩溃。这是我正在努力解决的程序的一部分,我想知道是否有人可以帮助我或提示我做错了什么?

char charToBin(char usersInput[]) {
    char c = " ";
    for (int i = 12; i >= 0; i--) {
        if((i == 0) || (i == 1) || (i == 3) || (i == 7)){
            usersInput[i] = usersInput[i + 1];
        }else{
            c = strtol(usersInput[i], (char **)NULL, 2);
        }
    }
    return c;
}

【问题讨论】:

    标签: c character-encoding binary ascii decode


    【解决方案1】:

    对于您的代码,您不能随意使用“strtol”。您提供给“strtol”的字符数组可能不以“\0”结尾。此外,不管你做什么,你的数组总是有 12 个索引,除非你将“\0”复制到索引 9,以便“strtol”知道它是输入的结尾。

    另外,有时循环不是最好的。对于您的情况,您已经知道您正在使用多少个索引。使用循环没有意义。尽管如此,我还是写了两个方法,并将测试代码作为示例包含在下面。

    #include <stdio.h>
    
    /*
     * This function generate a hammer binary digit string for testing.
     * It does not care about the validity of the hammer bit.
     * The array that is passed to this function should be the length of 12.
     */
    
    void generateChar(int value, char * output){
        output[0] = '0';
        output[1] = '0';
        output[3] = '0';
        output[7] = '0';
        output[2] =  (value & 0b10000000) > 0? '1' : '0';
        output[4] =  (value & 0b01000000) > 0? '1' : '0';
        output[5] =  (value & 0b00100000) > 0? '1' : '0';
        output[6] =  (value & 0b00010000) > 0? '1' : '0';
        output[8] =  (value & 0b00001000) > 0? '1' : '0';
        output[9] =  (value & 0b00000100) > 0? '1' : '0';
        output[10] = (value & 0b00000010) > 0? '1' : '0';
        output[11] = (value & 0b00000001) > 0? '1' : '0';   
    }
    
    /* 
     * First method.
     *
     */
    char charToBin(char usersInput[]) {
        char c = 0;
        c = usersInput[2] == '1'?  c | 0b10000000 : c;
        c = usersInput[4] == '1'?  c | 0b01000000 : c;
        c = usersInput[5] == '1'?  c | 0b00100000 : c;
        c = usersInput[6] == '1'?  c | 0b00010000 : c;
        c = usersInput[8] == '1'?  c | 0b00001000 : c;
        c = usersInput[9] == '1'?  c | 0b00000100 : c;
        c = usersInput[10] == '1'? c | 0b00000010 : c;
        c = usersInput[11] == '1'? c | 0b00000001 : c;
    
        return c;
    }
    
    /*
     * Second method.
     */
    char charToBin2(char usersInput[]) {
        char temp[9];
        int pos = 0;
        temp[8] = '\0'; // Protect from overflow.
    
        for ( int i = 2; i < 12; i++ ){
            if ( i == 3 || i == 7 ) continue;
            temp[pos] = usersInput[i];
            pos++;
        }
    
        return (char) strtol(temp, (char **)NULL, 2);
    }
    
    int main(){
        char a[] = "100010010001";
        char t[12];
        int b;
    
        // Test for method 1
        for ( int i = 0; i < 256; i++ ){
            generateChar(i, t);
            b = charToBin(t);
            printf("%d ", (unsigned char) b );
        }
    
        printf("\n\n");
    
        // Test for method 2
        for ( int i = 0; i < 256; i++ ){
            generateChar(i, t);
            b = charToBin2(t);
            printf("%d ", (unsigned char) b );
        }
    
        return 0;
    }
    

    【讨论】:

      【解决方案2】:
      if((i == 0) || (i == 1) || (i == 3) || (i == 7)){
                  usersInput[i] = usersInput[i + 1];
              }else{
      

      这里你的if(条件),后面的大括号是不必要的。

      if((i == 0) || (i == 1) || (i == 3) || (i == 7))
                  usersInput[i] = usersInput[i + 1];
              else{
      

      这可能会解决一些问题

      【讨论】:

        【解决方案3】:

        你的程序有两个编译错误:

        1. 您不能将字符串分配给字符 (c=" ") ;
        2. strtol 调用接受一个字符串,而不是一个字符

        修复编译错误后,需要对逻辑进行两处修复: 1. 对输入字符串进行从左到右的过滤,避免将位置12复制到11,将11复制到10,导致重复最后的位置。需要一个额外的计数器来帮助压缩。 2. 在输入完全压缩后执行一次strtol。

        char charToBin(char usersInput[]) {
            char j = 0 ;
            // Copy relevant input positions, INCLUDING terminating NUL byte at position 12.
            for (int i = 0; i <= 12 ; i++) {
                if((i == 0) || (i == 1) || (i == 3) || (i == 7)){
                        continue ;
                } ;
                usersInput[j] = usersInput[i] ;
                j++ ;
            } ;
            char c = strtol(usersInput, (char **)NULL, 2);
            return c;
        }
        
        

        【讨论】:

          【解决方案4】:

          或者,您可以使用位操作。比如:

          char charToBin(char usersInput[]) {
              unsigned c = strtol(usersInput, NULL, 2);
              unsigned part1 = c & 0xFu;
              unsigned part2 = c >> 1u & 0x70u;
              unsigned part3 = c >> 2u & 0x80u;
              return (char) (part1 | part2 | part3);
          }
          

          你的输入会给出

          char userInput[] = "100010010001";
          char ch = charToBin(userInput);
          printf("result: %c(%d)\n", ch, ch);
          

          控制台输出如下:

          result: A(65)
          

          【讨论】:

            猜你喜欢
            • 2012-03-30
            • 2016-10-02
            • 1970-01-01
            • 2017-06-03
            • 1970-01-01
            • 1970-01-01
            • 2014-06-14
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多