Commit | Line | Data |
---|---|---|
b35b6967 MM |
1 | #ifndef BIGINTEGERUTILS_H |
2 | #define BIGINTEGERUTILS_H | |
05780f4b MM |
3 | |
4 | #include "BigInteger.hh" | |
5 | #include <string> | |
6 | #include <iostream> | |
7 | ||
0afe80d5 MM |
8 | /* This file provides: |
9 | * - Convenient std::string <-> BigUnsigned/BigInteger conversion routines | |
10 | * - std::ostream << operators for BigUnsigned/BigInteger */ | |
05780f4b | 11 | |
0afe80d5 MM |
12 | // std::string conversion routines. Base 10 only. |
13 | std::string bigUnsignedToString(const BigUnsigned &x); | |
14 | std::string bigIntegerToString(const BigInteger &x); | |
15 | BigUnsigned stringToBigUnsigned(const std::string &s); | |
16 | BigInteger stringToBigInteger(const std::string &s); | |
05780f4b | 17 | |
a8b42b68 MM |
18 | // Creates a BigInteger from data such as `char's; read below for details. |
19 | template <class T> | |
0afe80d5 | 20 | BigInteger dataToBigInteger(const T* data, BigInteger::Index length, BigInteger::Sign sign); |
a8b42b68 | 21 | |
05780f4b MM |
22 | // Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'. |
23 | std::ostream &operator <<(std::ostream &os, const BigUnsigned &x); | |
24 | ||
25 | // Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'. | |
26 | // My somewhat arbitrary policy: a negative sign comes before a base indicator (like -0xFF). | |
27 | std::ostream &operator <<(std::ostream &os, const BigInteger &x); | |
28 | ||
0afe80d5 | 29 | // BEGIN TEMPLATE DEFINITIONS. |
a8b42b68 MM |
30 | |
31 | /* | |
6e1e0f2f MM |
32 | * Converts binary data to a BigInteger. |
33 | * Pass an array `data', its length, and the desired sign. | |
34 | * | |
35 | * Elements of `data' may be of any type `T' that has the following | |
36 | * two properties (this includes almost all integral types): | |
37 | * | |
38 | * (1) `sizeof(T)' correctly gives the amount of binary data in one | |
39 | * value of `T' and is a factor of `sizeof(Blk)'. | |
40 | * | |
41 | * (2) When a value of `T' is casted to a `Blk', the low bytes of | |
42 | * the result contain the desired binary data. | |
43 | */ | |
a8b42b68 | 44 | template <class T> |
0afe80d5 | 45 | BigInteger dataToBigInteger(const T* data, BigInteger::Index length, BigInteger::Sign sign) { |
a8b42b68 MM |
46 | // really ceiling(numBytes / sizeof(BigInteger::Blk)) |
47 | unsigned int pieceSizeInBits = 8 * sizeof(T); | |
48 | unsigned int piecesPerBlock = sizeof(BigInteger::Blk) / sizeof(T); | |
49 | unsigned int numBlocks = (length + piecesPerBlock - 1) / piecesPerBlock; | |
5ff40cf5 | 50 | |
a8b42b68 MM |
51 | // Allocate our block array |
52 | BigInteger::Blk *blocks = new BigInteger::Blk[numBlocks]; | |
5ff40cf5 | 53 | |
a8b42b68 | 54 | BigInteger::Index blockNum, pieceNum, pieceNumHere; |
5ff40cf5 | 55 | |
a8b42b68 | 56 | // Convert |
3260eb33 | 57 | for (blockNum = 0, pieceNum = 0; blockNum < numBlocks; blockNum++) { |
a8b42b68 | 58 | BigInteger::Blk curBlock = 0; |
3260eb33 | 59 | for (pieceNumHere = 0; pieceNumHere < piecesPerBlock && pieceNum < length; |
a8b42b68 MM |
60 | pieceNumHere++, pieceNum++) |
61 | curBlock |= (BigInteger::Blk(data[pieceNum]) << (pieceSizeInBits * pieceNumHere)); | |
62 | blocks[blockNum] = curBlock; | |
a8b42b68 | 63 | } |
5ff40cf5 | 64 | |
a8b42b68 MM |
65 | // Create the BigInteger. |
66 | BigInteger x(blocks, numBlocks, sign); | |
5ff40cf5 | 67 | |
a8b42b68 MM |
68 | delete blocks; |
69 | return x; | |
70 | } | |
71 | ||
05780f4b | 72 | #endif |