当源整数的大小是任意的时,数字系统之间是否有有效的转换算法?
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.
更多推荐
数字系统之间转换的高效算法
发布评论