cms架构:我需要用C++将二进制转换成16进制,然后再把16进制转成2进制,现在不知道如何写这样一个程序。...

来源:百度文库 编辑:中科新闻网 时间:2024/05/06 04:06:59

C语言版:
// zd_35.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void main()
{
long dnum;
char cnum[100];
printf("please input a decimal number:\n");
scanf("%d",&dnum);
ltoa(dnum,cnum,2);//把数子转换为2进制的字符串
printf("the binary number is:%s\n",cnum);
printf("the hex number is:%x\n",dnum);
}

运行结果:
please input a decimal number:
56
the binary number is:111000
the hex number is:38
Press any key to continue

C++版:
#include "stdafx.h"
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main(int argc, char* argv[])
{
int dnum;
char cnum[100];
cout<<"please input a decimal number:"<<endl;
cin>>dnum;
ltoa(dnum,cnum,2);//把数子转换为2进制的字符串
cout<<"the binary number is:"<<cnum<<endl;
cout<<"the hex number is:"<<hex<<dnum<<endl;
return 0;
}

运行结果:
please input a decimal number:
45
the binary number is:101101
the hex number is:2d
Press any key to continue