2013-09-26 71 views
0

我必須編寫無符號十六進制轉換成十進制,癸爲十六進制,並簽署十六進制分解和分解爲十六進制的程序,在不使用流過濾器「六角」和「DEC」。我的程序適用於無符號的十六進制轉換爲十進制和十進制到十六進制,但不適用於SIGNED十六進制到十進制和十進制到十六進制。任何幫助將不勝感激。先謝謝你!變換符號和無符號的十六進制來分解和分解成十六進制C++

#include <iostream> 
#include <string> 
#include <cmath> 
using namespace std; 

void convertDecToHex(unsigned hexNumber); 
int convertHexToDec(char hex[], unsigned numberSize); 
signed SignedDecToHex (signed DecNumberSigned); 
signed SignedHexToDec (int number); 

int main(){ 
    unsigned choice, numberSize = 0, hexNumber; 
    signed DecNumberSigned; 
    char hex[100000]; 
    string number, HexNumberSigned; 

    while (1){ 
     cout << "Enter 1 to convert from unsigned Dec to Hex:" << endl <<  "Enter 2 to convert from unsigned Hex to Dec:" 
      << endl << "Enter 3 to convert from signed Dec to Hex:"<< endl << "Enter 4 to convert from signed Hex to Dec:" << endl ; 
     cin >> choice; 

     if (choice == 1){ 
      cout << "Enter unsigned deC# to convert to hex: " << endl; 
      cin >> hexNumber; 
      convertDecToHex(hexNumber); 
     }//1 
     if (choice == 2){ 
      cout << "Enter unsigned hex # to convert to dec: " << endl; 
      cin >> number; 
      numberSize = number.size(); 
      strcpy(hex, number.c_str()); 
      cout << convertHexToDec(hex, numberSize) << endl; 
     }//2 
     if (choice == 3){ 
      cout << "Enter signed dec to convert to hex: " << endl; 
      cin >> DecNumberSigned; 
      convertDecToHex(SignedDecToHex(DecNumberSigned)); 
     }//3 
       if (choice == 4){ 
      cout << "Enter signed hex to convert to dec: " << endl; 
      cin >> HexNumberSigned; 
      numberSize = HexNumberSigned.size(); 
      strcpy(hex, HexNumberSigned.c_str()); 
      cout << SignedHexToDec(convertHexToDec(hex, numberSize)) <<  endl; 
     }//4 
    }//while 
    system ("pause"); 
}//main 

void convertDecToHex(unsigned hexNumber){ 
int remainder, i=0; 
    unsigned m[99999], total, x; 
    char n [99999]; 

    i=0; 
    do { 
     remainder = hexNumber % 16; 
     hexNumber /= 16; 
     m[i] = remainder; 
     i++; 
    }//doLooop 
    while (hexNumber > 0); 
    total = i; 

    for (i=0, x = total - 1; i < total; i++, x--){ 
     if (m[i] == 1) n[x] = '1'; 
     if (m[i] == 2) n[x] = '2'; 
     if (m[i] == 3) n[x] = '3'; 
     if (m[i] == 4) n[x] = '4'; 
     if (m[i] == 5) n[x] = '5'; 
     if (m[i] == 6) n[x] = '6'; 
     if (m[i] == 7) n[x] = '7'; 
     if (m[i] == 8) n[x] = '8'; 
     if (m[i] == 9) n[x] = '9'; 
     if (m[i] == 10) n[x] = 'A'; 
     if (m[i] == 11) n[x] = 'B'; 
     if (m[i] == 12) n[x] = 'C'; 
     if (m[i] == 13) n[x] = 'D'; 
     if (m[i] == 14) n[x] = 'E'; 
     if (m[i] == 15) n[x] = 'F'; 
    } 

    for (i=0; i < total; i++) 
     cout << n[i]; 
    cout << endl; 

}//DecToHex 
int convertHexToDec(char hex[], unsigned numberSize){ 
    int i, j; 
    int n[10000]; 
    unsigned sum = 0; 
    char a,b,c,d,e,f; 

    for (i = 0 ; i < numberSize; i++){ 
     if (hex[i] == '1') n[i] = 1; 
     if (hex[i] == '2') n[i] = 2; 
     if (hex[i] == '3') n[i] = 3; 
     if (hex[i] == '4') n[i] = 4; 
     if (hex[i] == '5') n[i] = 5; 
     if (hex[i] == '6') n[i] = 6; 
     if (hex[i] == '7') n[i] = 7; 
     if (hex[i] == '8') n[i] = 8; 
     if (hex[i] == '9') n[i] = 9; 
     if (hex[i] == 'a' || hex[i] == 'A') n[i] = 10; 
     if (hex[i] == 'b' || hex[i] == 'B') n[i] = 11; 
     if (hex[i] == 'c' || hex[i] == 'C') n[i] = 12; 
     if (hex[i] == 'd' || hex[i] == 'D') n[i] = 13; 
     if (hex[i] == 'e' || hex[i] == 'E') n[i] = 14; 
     if (hex[i] == 'f' || hex[i] == 'F') n[i] = 15; 

    }//forLoop 

    for (i = 0, j = numberSize - 1; i < numberSize , j >= 0 ; i++, j--) 
     sum += pow(16,i) * n[j]; 

    return sum; 

}//HexToDec 

signed SignedDecToHex (signed DecNumberSigned){ 
    signed convertNumber; 
    convertNumber = DecNumberSigned + (pow (2,16)); 
    return convertNumber; 
}// SignedDecToHex 

signed SignedHexToDec (int number){ 
    int newNumber; 
    newNumber = number - (pow (2,16)); 
    return newNumber; 

}//SingedHexToDec 
+0

你試過調試呢?從255無符號或-1有符號的東西開始小。 –

+0

你能提供一些樣本輸出嗎?所有的 –

+0

首先,你缺少的CString的和cstdlib包括...此外,你爲什麼不能使用十六進制/ DEC流過濾器? – lpapp

回答

1

做整數運算時不要使用pow()pow返回值作爲double這是不精確的,並且可能導致由值一個其轉換爲int時被關斷。

理論上雙打數爲二的冪的工作時,可能是準確的。但是,假設你知道你在做什麼,這在目前,你不:-)

更好地利用比特移位時可能:

pow(2,n) - >1 << n

如何使用bitshifting實現pow(16, n)保留爲鍛鍊; Tibial讀者。

提示:((I^N)^ M)= I ^(N * M)

0

我用一些這樣的:

long int dec = strtol("0x0A01006F", NULL, 16); // return 167837807 
printf("%d\n", dec); 
long int hex = strtol("167837807", NULL, 10); // return A01006F 
printf("%02X\n", hex); 
相關問題