2015-01-06 61 views
0

程序的目標是將十進制轉換爲十六進制 1)我有問題將整數值賦值給char數組元素如何完成。我得到我的數組數值的符號。 2)如何初始化我的數組到空格?擺脫數組元素中的垃圾和舊數據。 3)有人可以解釋我的問題是什麼?整數和字符? 4)cout是我的問題的一部分嗎?將整數值賦值給char數組元素

#include "stdafx.h" 
#include <iostream> 
#include <iomanip> 
#include <math.h> 

int main() 
{ 
using namespace std; 

// binary declarations 
int x, i, z, n, remainder; 
int result = 0; 
bool loop_cond, remainder_found, octval_found; 

// octal declarations 
int octalval,octresult,octremainder,temp_octalval; 
int octloop_cnt; 
bool oct_loop; 
octresult = 0; 
// hexadecimal declarations 
int hexval, hexremainder, temp_hexval; 
int hexloop_cnt; 
bool hex_loop,hexval_found; 
char hexresult[5] = {" "}; 

cout << "decimal" << "\t" << "binary" << "\t"<< "octal"<<"\t"<<"hexadecimal"<< endl; 

for (i = 1; i <= 256; i++){ 
hexloop_cnt = 0; 
octremainder = 0; 
hexval_found = false; 
hex_loop = true; 
hexloop_cnt = 0; 
while (hex_loop != false){ 

if (hexval_found != true){ 
    hexval = i/16; 
    hexremainder = i % 16; 
} 
else { 
    temp_hexval = hexval; 
    hexval = hexval/16; 
    hexremainder = temp_hexval % 16; 
} 


if (hexval == 0){ 
    switch (hexremainder){ 
    case 10: 
     hexresult[hexloop_cnt] = 'A'; 
     break; 
    case 11: 
     hexresult[hexloop_cnt] = 'B'; 
     break; 
    case 12: 
     hexresult[hexloop_cnt] = 'C'; 
     break; 
    case 13: 
     hexresult[hexloop_cnt] = 'D'; 
     break; 
    case 14: 
     hexresult[hexloop_cnt] = 'E'; 
     break; 
    case 15: 
     hexresult[hexloop_cnt] = 'F'; 
     break; 
    default: 
     hexresult[hexloop_cnt] = (char)hexremainder; 
     break; 
    }//end switch 
    hex_loop = false; 
}//end if 

if ((hexval < 16) && (hexval > 0)){ 
    switch (hexremainder){ 
    case 10: 
     hexresult[hexloop_cnt] = 'A'; 
     break; 
    case 11: 
     hexresult[hexloop_cnt] = 'B'; 
     break; 
    case 12: 
     hexresult[hexloop_cnt] = 'C'; 
     break; 
    case 13: 
     hexresult[hexloop_cnt] = 'D'; 
     break; 
    case 14: 
     hexresult[hexloop_cnt] = 'E'; 
     break; 
    case 15: 
     hexresult[hexloop_cnt] = 'F'; 
     break; 
    default: 
     hexresult[hexloop_cnt] = static_cast<char>(hexremainder); 
     break; 
    }//end switch 
    hexloop_cnt++; 
    switch (hexval){ 
    case 10: 
     hexresult[hexloop_cnt] = 'A'; 
     break; 
    case 11: 
     hexresult[hexloop_cnt] = 'B'; 
     break; 
    case 12: 
     hexresult[hexloop_cnt] = 'C'; 
     break; 
    case 13: 
     hexresult[hexloop_cnt] = 'D'; 
     break; 
    case 14: 
     hexresult[hexloop_cnt] = 'E'; 
     break; 
    case 15: 
     hexresult[hexloop_cnt] = 'F'; 
     break; 
    default: 
     hexresult[hexloop_cnt] = static_cast<char>(hexval); 
     break; 
    }//end switch 
    hex_loop = false; 
    } //end if 

if ((hexval >= 16)){ 
    switch (hexremainder){ 
    case 10: 
     hexresult[hexloop_cnt] = 'A'; 
     break; 
    case 11: 
     hexresult[hexloop_cnt] = 'B'; 
     break; 
    case 12: 
     hexresult[hexloop_cnt] = 'C'; 
     break; 
    case 13: 
     hexresult[hexloop_cnt] = 'D'; 
     break; 
    case 14: 
     hexresult[hexloop_cnt] = 'E'; 
     break; 
    case 15: 
     hexresult[hexloop_cnt] = 'F'; 
     break; 
    default: 
     hexresult[hexloop_cnt] = static_cast<char>(hexremainder); 
     break; 
    }//end switch 
    hexval_found = true; 
}//end if 

hexloop_cnt++; 
} //endwhile 


    cout << i << "\t" << result <<"\t" << octresult<<"\t" << hexresult<< endl; 
    result = 0; 
    octresult = 0; 
//  hexresult[5] = { ' ', ' ', ' ', ' ', ' ' }; 
} 



cin.clear(); 
cin.ignore(255, '/n'); 
cin.get(); 

return 0; 
} 
+3

請縮小代碼的相關部分,或者更好創建[最小,完整的,並且可驗證示例](http://stackoverflow.com/help/ mcve)並向我們展示。 –

+2

也縮進代碼。 +1給Joachim! – gsamaras

回答

1

你不能做到這一點

hexresult[hexloop_cnt] = (char)hexremainder; 

你需要做的

hexresult[hexloop_cnt] = hexremainder + '0'; 

爲什麼呢?好吧,如果hexremainder = 1。字符「1」的ascii不是0x01,它是0x31。所以你需要給這個值加上0x30。 '0'是0x30(看看一個ascii表,看看發生了什麼http://www.asciitable.com/

0

std :: cout可能是問題。以這個例子。

#include <iostream> 

int main() 
{ 
    unsigned char c = 239; 
    std::cout << c << "\n"; 
    std::cout << static_cast<int>(c) << "\n"; 

    return 0; 
} 

輸出:

./a.out 
� 
239