将整数值赋给char数组元素

时间:2015-01-06 17:31:57

标签: c++ arrays

程序目标是将十进制转换为十六进制 1)我在向char数组元素分配整数值时遇到问题。如何完成。我得到我的数组数值的符号。 2)如何将数组初始化为空格?摆脱数组元素中的垃圾和旧数据。 3)有人可以解释我的问题是什么吗?整数和字符? 4)cout是我问题的一部分吗?

#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;
}

2 个答案:

答案 0 :(得分:1)

你不能这样做

hexresult[hexloop_cnt] = (char)hexremainder;

你需要做

hexresult[hexloop_cnt] = hexremainder + '0';

为什么呢?好吧想想hexremainder = 1.字符的ascii&#34; 1&#34;不是0x01,它是0x31。所以你需要在值上加上0x30。 &#39; 0&#39;是0x30(查看ascii表以查看发生了什么http://www.asciitable.com/

答案 1 :(得分: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