C ++中的2的补码转换器

时间:2013-04-16 05:35:06

标签: c++ c++11

我陷入困境,我一直在看我的代码几个小时,我似乎无法弄清楚什么是错的。我的功能是它将采用负数或正数并将其转换为32位2的补码十六进制表示。我的函数接受一个字符数组(以32位有符号幅度表示)并取数组的2的补码。我将它转换为2的补码的方法是在看到某个字符时将一个字符分配给数组。如果在数组中看到的字符是第一个字符,则会出现特殊情况。其余的通常都是否定的。我现在卡住了。当我输入值-23(有符号幅度为80000017)时,我得到值K00000KK。 -23452(有符号幅度80005B9C) - > K000KKKK。我将值设置为'K'以找出代码被卡住的位置,原始值为'7'。在代码中,我放置显示消息以查看编译器的去向

for -23 (80000017) input I get the messages

first 1 is seen, leave first 1 the same
first 1 is seen, leave first 1 the same
no one's showed up yet --> 0
no one's showed up yet --> 0
no one's showed up yet --> 0
no one's showed up yet --> 0
no one's showed up yet --> 0
first 1 is seen, leave first 1 the same

几乎所有显示K的地方都是我获得前1条消息的地方,其中0显示的是我得到的 - > 0条消息

感谢您的帮助!非常感激!

这是我的代码的pastebin链接,因为代码的格式化让我感到烦恼......

http://pastebin.com/KSeymwcs

char* two_complement(int number, char* sign_mag_array){

int first_one_flag = 0;

if(number > 0){
    cout << "positive number" << endl;
    return sign_mag_array;
}

if(number < 0){
    for(int q = 7; q >= 0; q--){
        if(first_one_flag == 1){                // negate the rest regularly
            cout << "negate normally " << endl;

            if(sign_mag_array[q] == '0'){
                sign_mag_array[q] = 'F';
            }
            if(sign_mag_array[q] == '1'){
                sign_mag_array[q] = 'E';
            }
            if(sign_mag_array[q] == '2'){
                sign_mag_array[q] = 'D';
            }
            if(sign_mag_array[q] == '3'){
                sign_mag_array[q] = 'C';
            }
            if(sign_mag_array[q] == '4'){
                sign_mag_array[q] = 'D';
            }   
            if(sign_mag_array[q] == '5'){
                sign_mag_array[q] = 'A';
            }
            if(sign_mag_array[q] = '6'){
                sign_mag_array[q] = '9';
            }
            if(sign_mag_array[q] == '7'){
                sign_mag_array[q] = '8';
            }
            if(sign_mag_array[q] == '8'){
                sign_mag_array[q] = '7';
            }
            if(sign_mag_array[q] = '9'){
                sign_mag_array[q] = '6';
            }   
            if(sign_mag_array[q] == 'A'){
                sign_mag_array[q] = '5';
            }   
            if(sign_mag_array[q] == 'B'){
                sign_mag_array[q] = '4';
            }
            if(sign_mag_array[q] = 'C'){
                sign_mag_array[q] = '3';
            }
            if(sign_mag_array[q] == 'D'){
                sign_mag_array[q] = '2';
            }
            if(sign_mag_array[q] == 'E'){
                sign_mag_array[q] = '1';
            }
            if(sign_mag_array[q] == 'F'){
                sign_mag_array[q] = '0';
            }
        }


        if(sign_mag_array[q] == '0' && first_one_flag == 0){
            cout << "no one's showed up yet --> 0 " << endl;
        }
        else{                                               // first '1' in binary seen. special negating
            //first_one_flag = 1;               
            cout << "first 1 is seen, leave first 1 the same " << endl;

            if(sign_mag_array[q] == '1'){
                sign_mag_array[q] = 'F';        
            }
            if(sign_mag_array[q] == '2'){
                sign_mag_array[q] = 'E';
            }
            if(sign_mag_array[q] == '3'){
                sign_mag_array[q] = 'D';
            }
            if(sign_mag_array[q] == '4'){
                sign_mag_array[q] = 'C';
            }
            if(sign_mag_array[q] == '5'){
                sign_mag_array[q] = 'B';
            }
            if(sign_mag_array[q] == '6'){
                sign_mag_array[q] = 'A';
            }
            if(sign_mag_array[q] = '7'){
                sign_mag_array[q] = '9';
            }
            if(sign_mag_array[q] == '8'){
                sign_mag_array[q] = '8';
            }
            if(sign_mag_array[q] == '9'){
                sign_mag_array[q] = 'K';
            }       
            if(sign_mag_array[q] == 'A'){
                sign_mag_array[q] = '6';
            }   
            if(sign_mag_array[q] == 'B'){
                sign_mag_array[q] = '5';
            }
            if(sign_mag_array[q] == 'C'){
                sign_mag_array[q] = '4';
            }
            if(sign_mag_array[q] == 'D'){
                sign_mag_array[q] = '3';
            }
            if(sign_mag_array[q] == 'E'){
                sign_mag_array[q] = '2';
            }
            if(sign_mag_array[q] == 'F'){
                sign_mag_array[q] = '1';
            }
        }
    }    
}
return sign_mag_array;

}

2 个答案:

答案 0 :(得分:4)

错误很简单

        if(sign_mag_array[q] = '7'){
            sign_mag_array[q] = '9';
        }

应该是

        if(sign_mag_array[q] == '7'){
            sign_mag_array[q] = '9';
        }

        if(sign_mag_array[q] = 'C'){
            sign_mag_array[q] = '3';
        }

应该是

        if(sign_mag_array[q] == 'C'){
            sign_mag_array[q] = '3';
        }

        if(sign_mag_array[q] = '9'){
            sign_mag_array[q] = '6';
        }   

应该是

        if(sign_mag_array[q] == '9'){
            sign_mag_array[q] = '6';
        }   

等。我认为你明白了,

这种重复的代码是糟糕的风格,并且使这样的错误变得容易。您应该能够通过一些数学来大大简化和缩短此代码。

例如,添加两个函数将十六进制数字转换为整数,反之亦然

int hex_to_int(char x)
{
   return x <= '9' ? x - '0' : 10 + (x - 'A');
}

char int_to_hex(int x)
{
   return x < 10 ? '0' + x : 'A' + (x - 10);
}

现在这样的代码

       if(sign_mag_array[q] == '0'){
            sign_mag_array[q] = 'F';
        }
        if(sign_mag_array[q] == '1'){
            sign_mag_array[q] = 'E';
        }
        ...
        if(sign_mag_array[q] == 'E'){
            sign_mag_array[q] = '1';
        }
        if(sign_mag_array[q] == 'F'){
            sign_mag_array[q] = '0';
        }

变为

     if (sign_mag_array[q] >= '0' && sign_mag_array[q] <= '9' || 
         sign_mag_array[q] >= 'A' && sign_mag_array[q] <= 'F')
     {
         int digit = hex_to_int(sign_mag_array[q]);
         sign_mag_array[q] = int_to_hex(15 - digit);
     }

这48行重复代码减少到很少。作为奖励,它还解决了Zeta发现的问题。

答案 1 :(得分:2)

除了john所说的,你的逻辑目前是错误的。请看下面的代码段:

    if(sign_mag_array[q] == '1'){
        sign_mag_array[q] = 'F';        
    }
    /* ... */
    if(sign_mag_array[q] == 'F'){
        sign_mag_array[q] = '1';
    }

如果sign_mag_array[q]'1',您会将其设为'F'。这将导致后一条件成立,因此sign_mag_array[q]被重置为'1'

您需要独占条件:

    if(sign_mag_array[q] == '1'){
        sign_mag_array[q] = 'F';        
    }
    else if(sign_mag_array[q] == '2'){
        sign_mag_array[q] = 'E';        
    }
    /* ... */
    else if(sign_mag_array[q] == 'F'){
        sign_mag_array[q] = '1';
    }

由于你已经开始独家,你可以简单地使用switch而不是长序列的ifs:

switch(sign_mag_array[q]){
    case 'F': sign_mag_array[q] = '1'; break;
    case 'E': sign_mag_array[q] = '2'; break;
    /* ... */
    case '1': sign_mag_array[q] = 'F'; break;
}