十六进制转换为十进制当没有数据类型可以保存完整号码 [英] Convert Hex to Decimal when no datatype can hold the full number

查看:175
本文介绍了十六进制转换为十进制当没有数据类型可以保存完整号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我正与一个PIC单片机,在C.这是16F,所以它不能持有整数大于32位的(无符号INT32提供最大的数据大小)

Ok, so I am working with a PIC microprocessor, in C. It's a 16F, so it can't hold integers larger than 32bits (unsigned int32 is the largest datasize available)

这是一个读者,我收到了5个字节的ID code。进行传递,我一定要带连接coded到BCD码,数字一个数字。我不能冲刺它为一个字符串,因为它是大的数据大小,并且不能处理它。我不能把它,因为没有操作定义它。

From a reader, I receive a 5 byte ID code. To transmit it, I have to encoded to BCD, digit by digit. I can't sprint it to a string, as it is larger that the data size, and can't process it. I can't divide it because no operation is defined for it.

我想不出任何可能的解决方案,没有任何人有处理过?

I can't figure out any solution possible, does anyone have dealt with this before?

编辑:

我接收在一系列的5个字节的数目:FF-FF-FF-FF-FF。我需要将其转换为十进制0123456789012(13位,256 ^ 5十进制长度),它通过RS232发送。第二个函数(以ASCII码,并将其发送)我已经有工作,但我需要重新串的全部数presentation之前,我可以用它做什么。

I receive the number in a series of 5 bytes: "FF-FF-FF-FF-FF". I need to convert it to decimal "0123456789012" (13 digits, length of 256^5 in decimal) to send it through RS232. The second function (Take the ASCII, and send it) I already have it working, but I need the string representation of the full number before I can do anything with it.

推荐答案

假设你有32位运算:2 ** 24 = 16777216,因此服用x作为最显著2个字节和y为至少显著3:

Assuming you have 32 bit arithmetic: 2**24 = 16777216, so taking x as the most significant 2 bytes and y as the least significant 3:

  (16777216 * x + y) / 1000 
= (16777000 * x + 216 * x + y) / 1000
= 16777 * x + (216 * x + y) / 1000

第一项可以在不溢出来计算在32位(因为 X 2 ** 16 )。 第二项也可以不溢出(因为计算 X 2 ** 16 和 Y'2 ** 24 )。

The first term can be calculated in 32 bits without overflow (since x < 2**16). The second term can also be calculated without overflow (since x < 2**16 and y < 2**24).

这基本上是长除法的基础 2 ** 24 2位数的价值,但条件pre-计算明知除数为1000。一干就是选择,因为它是10更大的最少的功率比 2 ** 8

This is basically long division in base 2**24 of a 2-digit value, but with terms pre-calculated knowing that the divisor is 1000. One thousand is chosen because it's the least power of 10 greater than 2**8.

因此​​,首先计算最低的三个数字,用事实(2 ** 32)%1000 == 296 。所以这一次,我们将采取x作为最高字节和y作为低4个字节

So, first compute the lowest three digits, use the fact that (2**32) % 1000 == 296. So this time we'll take x as the highest byte and y as the low 4 bytes

((2**32) * x + y) % 1000 = ((2**32) * x) % 1000 + y % 1000 (modulo 1000)
                         = (296 * x) % 1000 + y % 1000     (modulo 1000)
((2**32) * x + y) % 1000 = ((296 * x) % 1000 + y % 1000) % 1000

然后1000使用上面的公式划分原来的号码。然后你安全地进入32位的领土,并且可以使用正常的循环生产出剩余的数字。

Then divide the original number by 1000 using the formula above. Then you're safely into 32 bit territory and can churn out the remaining digits using the normal loop.

顺便说一句,我会检查的结果,如果我是你,我没有测试过这一点,这是可能的,我什么地方犯了一个错误。应该很容易进行比较的的BCD转换结果使用通常的装置在PC上的64位整数进行。

Btw, I'd check the results if I were you: I haven't tested this and it's possible I've made an error somewhere. It should be easy to compare against the results of bcd conversions done using the usual means in a 64-bit integer on a PC.

这篇关于十六进制转换为十进制当没有数据类型可以保存完整号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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