数字系统之间转换的高效算法

编程入门 行业动态 更新时间:2024-10-13 16:17:51
本文介绍了数字系统之间转换的高效算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

更多推荐

数字系统之间转换的高效算法

本文发布于:2023-11-29 21:18:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647656.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:高效   算法   数字   系统

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!