将整数值分配给 char 数组元素
assigning integer values to char array elements
程序目标是将十进制转换为十六进制1)我在将整数值分配给字符数组元素时遇到问题。我正在获取我的数组数值的符号。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;
}
你不能这样做
hexresult[hexloop_cnt] = (char)hexremainder;
你需要做
hexresult[hexloop_cnt] = hexremainder + '0';
为什么?好吧,想想如果十六进制= 1。字符"1"的 ascii 不是0x01而是0x31。因此,您需要向该值添加0x30。"0"表示0x30(查看 ASCII 表以了解 http://www.asciitable.com/发生了什么)
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
相关文章:
- 为char数组调整zlib-zpipe
- 使用strcpy将char数组的元素复制到另一个数组
- 如果我在相应 char 数组的声明中为其提供额外的元素,是否会自动设置 NULL?
- 常量数组如何在每个元素中只能包含字符,而 char* 数组能够指向每个元素中的字符串?
- 为什么在 c++ 中分配 char 数组元素时,分配的字符会被销毁?
- C++函数返回两个 char 数组的相同索引元素
- 将双插入char*数组元素中
- c ++ 我的 char* 数组被最后一个添加的元素覆盖
- 使用for循环将char数组的一个元素复制到另一个元素
- 如何设置变量的 char 数组的元素数量
- 将整数值分配给 char 数组元素
- 如何在 c++ 中将 char 数组元素传递给函数
- 如何获取char数组中元素的地址
- 如何连接char数组元素并将其强制转换为float
- 将两个单词读入一个char数组元素
- 将Char数组中的元素复制到short中
- 我需要连接一个unsigned char数组的两个元素并保持它为unsigned char
- 从char*访问数组元素
- 用const char初始化2D char数组元素
- 连接一个char 2D数组元素和c++中的int