c++程序输出结果为什么是这个?
来源:百度知道 编辑:UC知道 时间:2024/06/07 18:12:07
//这个程序把10进制换成BCD码
#include <iostream.h>
void main()
{
unsigned long Dec =117; // 十进制数
unsigned char *Bcd = new unsigned char[2]; // BCD码
int length = 2; // BCD码长度
// 以下为转换算法
int i;
int temp;
for(i=length-1; i>=0; i--)
{
temp = Dec%100;
Bcd[i] = ((temp/10)<<4) + ((temp%10) & 0x0F);
Dec /= 100;
cout<<"Bcd["<<i<<"]="<<Bcd[i]<<endl;
}
cout<<"Bcd="<<Bcd<<endl;
}
117的BCD码应该是0000 0001 0001 0111
就是BCD[1]0000 0001
BCD[0]0001 0111
为什么输出为:
Bcd[1]=
Bcd[0]=
Bcd=??
Press any key to continue
很奇怪的符号,我复制到这里就又变了,其中有一个是笑脸?
#include <iostream.h>
void main()
{
unsigned long Dec =117; // 十进制数
unsigned char *Bcd = new unsigned char[2]; // BCD码
int length = 2; // BCD码长度
// 以下为转换算法
int i;
int temp;
for(i=length-1; i>=0; i--)
{
temp = Dec%100;
Bcd[i] = ((temp/10)<<4) + ((temp%10) & 0x0F);
Dec /= 100;
cout<<"Bcd["<<i<<"]="<<Bcd[i]<<endl;
}
cout<<"Bcd="<<Bcd<<endl;
}
117的BCD码应该是0000 0001 0001 0111
就是BCD[1]0000 0001
BCD[0]0001 0111
为什么输出为:
Bcd[1]=
Bcd[0]=
Bcd=??
Press any key to continue
很奇怪的符号,我复制到这里就又变了,其中有一个是笑脸?
你输出的是char字符,而不是以二进制的形式输出地
你把文件头部改为
#include <iostream>
#include <bitset>
using namespace std;
把for循环里面的输出语句改为
cout<<"Bcd["<<i<<"]="<<bitset<sizeof(char)*8>(Bcd[i])<<endl;