二进制,八进制,十进制和十六进制 之间的关系转换

论坛上看到好多帖子在讨论各种进制之间的相互如何转换。感觉这个那个计算器点几下就能搞定了,没啥高深的原理在里面。

当然点鼠标大家都会,可能转换到C语言,代码对这些进制进行转换的时候会遇到这个或者那个的问题。


这里简单写了一个各种进制之间的转换函数,当然目前仅实现了二进制,八进制,十进制和十六进制,而且关于扫描输入这块也并非完善,只是简单提供一个reference code,其他有兴趣的朋友可以补充完善。


Binary
二进制: 0, 1   
Octal                              
八进制: 0, 1, 2, 3, 4, 5, 6, 7
Digital
十进制: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 
heX
十六进制: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f


这里给出一个简单的工具,其命令如下:



格式:./transform <命令> <字符串>, 如果命令字段给出'b' 用于2进制输入;'o'用于八进制输入;'d'用于十进制输入;'x'用于十六进制输入。


与字符串中提取数值类似的方法【1】

字符串提取二进制输入:

int skip_atob(char **s)
{
	int i=0;
	int b=0;
	printf("scan string %s, char 0x%x, num = 0x%x, i = %d\n", *s, **s, **s - '0', i);
	while (isdigit(**s)) /* Fix me: judge '1' and '0' only */
	{
#if (ENABLE_DEBUG)
		printf("binary found\n");
#endif /* ENABLE_DEBUG */
		b = *((*s)++) - '0';
		if (b > 1 || b < 0)
		{
			return i;
		}
		
		i = i*2 + b;
	}
	return i;
}

字符串提取八进制输入:

int skip_atoo(char **s)
{
	int i=0;
	int b=0;
	printf("scan string %s, char 0x%x, num = 0x%x, i = %d\n", *s, **s, **s - '0', i);
	while (isdigit(**s)) /* Fix me: judge '0' though '7' only */
	{
#if (ENABLE_DEBUG)
		printf("octal found\n");
#endif /* ENABLE_DEBUG */
		b = *((*s)++) - '0';
		if (b > 8 || b < 0)
		{
			return i;
		}
		
		i = i*8 + b;
	}
	return i;
}

字符串提取十进制输入:

int skip_atoi(char **s)
{
	int i=0;
	printf("scan string %s, char 0x%x, num = 0x%x, i = %d\n", *s, **s, **s - '0', i);
	while (isdigit(**s)) 
	{
#if (ENABLE_DEBUG)
		printf("digit found\n");
#endif /* ENABLE_DEBUG */
		i = i*10 + *((*s)++) - '0';
	}
	return i;
}

字符串提取十六进制输入:

int skip_atoh(char **s)
{
	int i=0;
	int b=0;
	printf("scan string %s, char 0x%x, num = 0x%x/0x%x, i = %d\n", 
	       *s, **s, **s - '0', **s - 'a', i);
	while (isxdigit(**s)) /* Fix me: judge '0' though 'f' only, lowcase should be considered. */
	{
#if (ENABLE_DEBUG)
		printf("hex found\n");
#endif /* ENABLE_DEBUG */
		if (isdigit(**s))
		{
			b = *((*s)++) - '0';
		}else
		{
			b = *((*s)++) - 'a' + 10;
		}

		if (b > 15 || b < 0)
		{
			return i;
		}
		
		i = i*16 + b;
	}
	return i;
}


不同进制输入都将被转换成十进制数值保存,然后再将十进制转换到各个进制对应的输出:

十进制转换到二进制:

int ConvertDecimal2Binary(int d)
{
	int iret;
	
	printf(" Binary system:");
	iret = scale(d, 2);
	printf("\r\n");
	return iret;
}

十进制转换到八进制:

int ConvertDecimal2Octal(int d)
{
	int iret;
	
	printf("  Octal system:");
	iret = scale(d, 8);
	printf("\r\n");
	return iret;
}

十进制转换到十六进制:

int ConvertDecimal2Hex(int d)
{
	int iret;
	
	printf("    Hex system:");
	iret = scale(d, 16);
	printf("\r\n");
	return iret;
}

进制转换核心代码scale:

/* convert from decimal to other systems */
static int scale(int d,int base)
{
	if (base > sizeof(g_apcTags)/4)
	{
		printf("Error: system(%d) is above %d.\r\n", 
		        base, sizeof(g_apcTags)/4);
		return -1;
	}
	
	if (NULL == *(g_apcTags + base - 1))
	{
		printf("Error: system(%d) is NOT supported yet.\r\n", 
		        base);
		return -2;
	}
	
    if(d/base!=0)
    {
        scale(d/base,base);
        printf("%c", *(*(g_apcTags + base - 1) + d%base + 1));
    }
    else
    {
        printf("%c", *(*(g_apcTags + base - 1) + d%base + 1));
    }
	
	return 0;
}

测试二进制输入:


测试八进制输入:


测试十进制输入:


测试十六进制输入:



参考资料:

【1】【C语言】从字符串中简单提取数值

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值