进制转换

类别:编程语言 点击:0 评论:0 推荐:


例子一:转成二进制

#include<iostream.h>
char a[]="01";
void tentotwo(unsigned int n)
{
 if(n)
 {
  tentotwo(n/2);
     cout<<a[n%2];
 }
}

void main()
{
 unsigned int m;
 cin>>m;
 tentotwo(m);

}


 

例子二 转成16进制

#include <iostream.h>
#include <string>

std::string DecToBin(unsigned int value)
{
 unsigned int dm = 1 << 15;

 char bin[17] = "";

 for (int i = 1; i <= 16; i++)
 {
  bin[i - 1] = ( value & dm ? '1' : '0' );
  value <<= 1;
 }
 return std::string(bin);
}

void main()
{
 cout << DecToBin(960).c_str() << endl;
}

本文地址:http://com.8s8s.com/it/it24491.htm