如何转换小数回BCD? [英] How to convert decimal back BCD?

查看:158
本文介绍了如何转换小数回BCD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够BCD转换为十进制,比如我可以转换为0x11到11,而不是17(十进制)。这是code口使用。

I am able to convert BCD to decimal, for example I can convert 0x11 to 11 instead of 17 in decimal. This is the code I used.

unsigned char hex = 0x11;
unsigned char backtohex ;

int dec = ((hex & 0xF0) >> 4) * 10 + (hex & 0x0F);

现在我想转换十二月回BCD重新presentation。我想11改回为0x11不0x0B中。我是怎么回去那种迷茫。

Now I want to convert dec back to BCD representation. I want 11 to be converted back to 0x11 not 0x0B. I am kind of confused as how to go back.

谢谢!

推荐答案

假设你输入始终为0和99之间,包括:

Assuming your input is always between 0 and 99, inclusive:

unsigned char hex = ((dec / 10) << 4) | (dec % 10);

简单地采取了高位倒腾它由一个半字节左,或的到位低位。

这篇关于如何转换小数回BCD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆