数字系统之间转换的高效算法 [英] Efficient algorithm for conversion between numeral system

查看:92
本文介绍了数字系统之间转换的高效算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当源整数的大小是任意的时,数字系统之间是否有有效的转换算法?

Is there any efficient algorithm for conversion between numeral system when the size of source integer is arbitrary?

例如,假设有一个整数数组{1, 4,8是以十进制格式作为输入的148。它可能以十六进制格式转换为{9,4},或以八进制格式转换为{2,2,4},或以二进制格式转换为{1,0,0,1,0,1,0,0},或仅{以1234-ary格式或其他东西。

For example, assume that there is an integer array {1, 4, 8} which is 148 in decimal format as an input. It might be converted to {9, 4} in hexadecimal format, or {2, 2, 4} in octal, or {1, 0, 0, 1, 0, 1, 0, 0} in binary format, or just {148} in 1234-ary format or something.

当实际值可以用机器支持的字大小表示时,这很简单。但当它达到任意大小时,我找不到比O(n ^ 2)更有效的方法。

It's simple when the actual value can be expressed in word-size supported by machine. But when it goes to arbitrary size, I cannot find efficient way better than O(n^2).

推荐答案

除以基础,推回模块,冲洗并重复商数!= 0。

Divide by the base, push back the module, rinse and repeat while quotient != 0.

因此,例如在基数16中转换148

So, for example convert 148 in base 16

148 / 16 = 9 r 4
  9 / 16 = 0 r 9

因此,十六进制中的148是0x94。这不应该花这么长时间。

So, 148 in hex is 0x94. This shouldn't take so long.

这篇关于数字系统之间转换的高效算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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