什么是一个BIGNUM型结构转换为可读字符串的有效途径? [英] What is an efficient way to convert a bignum type structure to a human readable string?

查看:909
本文介绍了什么是一个BIGNUM型结构转换为可读字符串的有效途径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点问题。为了能在我的C知识成长,我决定尝试实施基本的BIGINT库。

I've got a bit of a problem. In order to grow in my knowledge of C, I've decided to try to implement a basic bigint library.

在BIGINT结构的核心将是32位整数数组,选择,因为他们将适合在寄存器中。这将让我的数字,将在64位整数(也适合在寄存器中,因为我对的x86-64)之间产生溢出做业务,我可以bitshift出结果的各个部分。我已经实现了基本的加法,并测试它的工作,我必须打印阵列。对于我自己的测试目的,如果我用的printf()和输出的每个数字以十六进制它的罚款。我可以读取就好了。

The core of the bigint structure will be an array of 32 bit integers, chosen because they will fit in a register. This will allow me to do operations between digits that will overflow in a 64 bit integer (which will also fit in a register, as I'm on x86-64), and I can bitshift out each part of the result. I've implemented basic addition, and to test that it is working, I have to print the array. For my own testing purposes, it's fine if I use printf() and output each digit in hex. I can read that just fine.

然而,大多数人无法读取的十六进制。由于号码存储在(本质)基地2 ^ 32,印刷是有点问题的。什么是对基地10转换的好办法?

However, most people can't read hex. As the number is stored in (essentially) base 2^32, printing is a bit of a problem. What would be a good way to convert to base 10?

编辑:

此交易不知道如何从基本转换为基地,而是一个很好的方式来实现这一点。我正沿着制作与转换为另一种印刷基地的另一个BIGINT的思行

This deals not with knowing how to convert from base to base, but about a good way to implement this. I was thinking along the lines of making another bigint with another base with conversion for printing.

推荐答案

首先,你不能这样做没有我的基本业务/以一种合理的方式O(例如,除法和模量)。为了提供高效的执行转换BIGINT立足-10串,我研究两种可能的优化:

First of all, you can't do I/O in a sensible way without the basic operations(e.g. division and modulus). To provide efficient implementation of converting the bigint to base-10 string, I am researching two possible optimizations:

首先,你可以十倍,而不是十恰好是一些权力划分。这意味着什么,你会得到四种基本-10每次10000例如把数时间数字。

First, you can divide by some power of ten instead of ten exactly. What that means, you will get four base-10 digits every time you divide the number by 10000 for example.

二,你会怎么选择通过划分其中十个权力? 10,100,1000,10000等..结果
似乎有一个不错的选择这是十的最大功率,可以适应你的字(32位)。幸运的是,你可以更有效地实现一个分词/模量比当谈到两个BIGINTS。

Second, how would you choose which power of ten to divide by? 10, 100, 1000, 10000, etc...
There seems to be a good choice which is the maximum power of ten that can fit in your word(32-bit). Fortunately, you can implement division/modulus by one word much more efficiently than when it comes to two "bigint"s.

我没有给予实现,因为我仍然在研究我的业余时间的问题,因为我已经实现了我的图书馆,我的基本操作/ O是希望下一步;)

I haven't given an implementation because I am still researching the problem in my spare time because I have implemented the basic operations in my library and I/O is the next step hopefully ;)

这篇关于什么是一个BIGNUM型结构转换为可读字符串的有效途径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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