| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4523 人关注过本帖, 1 人收藏
标题:求2进制10进制16进制之间互换的C语言代码
只看楼主 加入收藏
monk02698
Rank: 1
等 级:新手上路
帖 子:13
专家分:7
注 册:2010-5-31
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:4 
求2进制10进制16进制之间互换的C语言代码
要求:首先要给出三个选择,1:表示输入的是2进制,2:表示输入的是10进制,3:表示输入的是16进制的数;
输完之后,敲回车输出其它的两个进制。
其中在输入的时候如果输入格式不对要有提示重新输入。
搜索更多相关主题的帖子: 代码 C语言 进制 
2010-06-03 22:17
monk02698
Rank: 1
等 级:新手上路
帖 子:13
专家分:7
注 册:2010-5-31
收藏
得分:0 
这里有一个
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
typedef unsigned int uint;
typedef unsigned short ushort;
typedef unsigned char uchar;
typedef unsigned long long ullong;
typedef uchar boolean;
#define TRUE 1
#define FALSE 0
char *DecToBin(uint dec){
    int i=0;
    char *str=NULL;
    boolean start=FALSE;
    uchar bit_size=0;
    boolean b;
    for(i=sizeof(uint)*8-1;i>=0;i--){
        b=(dec>>i)&1;
        if(start){
            str[bit_size-i-1]=b?'1':'0';
        }else if(b){
                start=TRUE;
                bit_size=i+1;
        printf("bit_size:%u\n",bit_size);
                str=(char *)malloc(bit_size+1);
                str[bit_size-i-1]=(b)?'1':'0';
        }
    }

    if(!start){
        bit_size=1;
        str=(char *)malloc(bit_size+1);
        str[0]='0';
    }
    str[bit_size]='\0';



    return str;
}
int main()
{
    uint i;
    char *str;
    for(i=0;i<100;i++)
    {
        str=DecToBin(i);
        printf("%u => %s\n",i,str);
        free(str);
    }
}
只是转换为2进制的
2010-06-04 00:02
zhuxu0423
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:59
专家分:101
注 册:2010-4-12
收藏
得分:10 
2进制 -----> 16进制

   2进制先转换为10进制,再转换为16进制

    CString strDec, strBin, strHex;
    strBin = _T("1110");
    strDec = BinToDec(strBin);
    int nDec;
    nDec = atol(strDec);
    strHex.Format(_T("%x"), nDec);   

 10 进制 -----> 16 进制

    int nDec = 10;
    CString str;
    str.Fomat(_T("%x"), nDec);

16 进制 -----> 10 进制

    CString strDec, strHex;
    strHex = _T("af");
    DWORD dwHex = strtoul(strHex, NULL, 16);
    strDec.Format(_T("%ld"), dwHex);

16 进制 -----> 2 进制

    16进制先转换为10进制,再转换为2进制
   
    CString strDec, strBin, strHex;
    strHex = _T("af");
    DWORD dwHex = strtoul(strHex, NULL, 16);
    strDec.Format(_T("%ld"), dwHex);
    strBin = DecToBin(strDec);

2010-06-04 18:31
kettle99
Rank: 2
等 级:论坛游民
帖 子:52
专家分:54
注 册:2010-5-25
收藏
得分:10 
#include "stdio.h"
#include "math.h"
main()
{
  int error,i,j,k,number_b,number_d,temp;
 char type;
 char number_copy[60],number[60];
 error=1;
 number_d=0;
 printf("please choose type,1--Binery,2--Decimal,3--Hex\n");
 scanf("%c",&type);
 while((type!='1')&&(type!='2')&&(type!='3'))
   {
       printf("type error,please input the type again\n");
       scanf("%c",&type);
   }
 
 
 

 
switch (type)
   {
       case '1':
           {
               printf("\nplease input the binery number:");
               scanf("%s",number);
        /*判断二进制输入是否合法,不合法重新输入*/      
               while (error==1)
                 {
                   for(i=0;number[i]!='\0';i++)
                     {
                         if((number[i]!='0')&&(number[i]!='1'))
                            {
                                error=1;
                                break;
                            }
                         else
                         error=0;
                     }
                  if(error==0)
                  break;
                  else
                     {
                           printf("\nnumber error,pls input again:\n");
                           scanf("%s",number);
                     }
                 }
         
  /*输入的二进制转换为十进制和十六进制输出*/        
          for(i=i-1,j=0;i>=0;i--,j++)
            {
                number_d=(number[j]-48)*pow(2,i)+number_d;
               }
          printf("\nthe decimal number is:%d\nthe hex number is:%X",number_d,number_d);     
           }
       break;
      
      
       case '2':
           {
               printf("\nplease input the decimal number:");
               scanf("%s",number);
        /*判断二进制输入是否合法,不合法重新输入*/  
            
             while(error==1)
               {
                   for(i=0;number[i]!='\0';i++)
                     {
                       if(number[i]<'0'||number[i]>'9')
                         {
                             error=1;
                        break;
                         }
                       else
                       error=0;
                     }
                   if(error==0)
                   break;
                   else
                   {
                           printf("\nnumber error,pls input again:\n");
                           scanf("%s",number);
                     }
               }
           
/*输入的十进制转换为二进制和十六进制输出*/        
          for(i=i-1,j=0;i>=0;i--,j++)
            {
                number_d=(number[j]-48)*pow(10,i)+number_d;
            }
          printf("\nthe hex number is:%X",number_d);  
         
          for(i=0;number_d!=0;i++)
            {
                number[i]=number_d%2+48;
                number_d=number_d/2;
               }         

          for(j=i-1,k=0;k<(i-1)/2+1;j--,k++)
            {
                temp=number[j];
                number[j]=number[k];
                number[k]=temp;
               }
           
          printf("\nthe binary number is:%s",number);   
           }
       break;
      
      
       case '3':
           {
               printf("\nplease input the hex number:");
               scanf("%s",number);
               /*判断十六进制输入是否合法,不合法重新输入*/
               while(error==1)
               {
                   for(i=0;number[i]!='\0';i++)
                     {
                       if((number[i]>='0'&&number[i]<='9')||(number[i]>='a'&&number[i]<='f')||(number[i]>='A'&&number[i]<='F'))
                         {
                             error=0;
                         }
                       else
                        {
                            error=1;
                            break;
                        }
                     }
                   if(error==0)
                   break;
                   else
                   {
                           printf("\nnumber error,pls input again:\n");
                           scanf("%s",number);
                     }
               }
          /*输入的十六进制转换为二进制和十进制输出*/
         
                  for(i=i-1,j=0;i>=0;i--,j++)
                {
                    if(number[j]>='0'&&number[j]<='9')
                    number_d=(number[j]-48)*pow(16,i)+number_d;
                    else if(number[j]>='a'&&number[j]<='f')
                    number_d=(number[j]-87)*pow(16,i)+number_d;
                    else if(number[j]>='A'&&number[j]<='F')
                    number_d=(number[j]-55)*pow(16,i)+number_d;
                }
            
              printf("\nthe decimal number is:%d",number_d);  
              
              for(i=0;number_d!=0;i++)
                {
                    number[i]=number_d%2+48;
                    number_d=number_d/2;
                }      
         
            
          for(j=i-1,k=0;k<(i-1)/2+1;j--,k++)
            {
                temp=number[j];
                number[j]=number[k];
                number[k]=temp;
               }
         
           
          printf("\nthe binary number is:%s",number);     
           }
       break;
      
       }
 
 }

我这个有错误输入提示,且输入范围错误后会弹出重新输入,测试正确
2010-06-05 17:24
monk02698
Rank: 1
等 级:新手上路
帖 子:13
专家分:7
注 册:2010-5-31
收藏
得分:0 
太感动了,谢谢楼上的
2010-06-11 19:29
快速回复:求2进制10进制16进制之间互换的C语言代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.049591 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved