2015-01-06 7 views
0

프로그램 목적은 십진수를 16 진수로 변환하는 것입니다.1) char 배열 요소에 정수 값을 할당하는 데 문제가 있습니다. 내 배열 값에 대한 기호를 가져 오는 중이 야. 2) 배열을 공백으로 어떻게 초기화합니까? 배열 요소에서 가비지 및 오래된 데이터를 제거합니다. 3) 누군가 내 문제가 무엇인지 설명 할 수 있습니까? 정수와 문자로? 4) 귀찮은 문제가 있습니까?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 요아킴! – gsamaras

답변

1

이이

hexresult[hexloop_cnt] = (char)hexremainder; 

당신이

hexresult[hexloop_cnt] = hexremainder + '0'; 

이유를 어떻게해야합니까 캔트? 만약 hexremainder = 1이라고 생각하면됩니다. 문자 "1"에 대한 ascii는 0x01이 아니며 0x31입니다. 따라서 값에 0x30을 추가해야합니다. '0'은 0x30입니다 (무슨 일이 일어나고 있는지 보려면 ascii 표를보십시오)

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