diff options
| author | Kyle K <kylek389@gmail.com> | 2011-02-18 19:10:16 -0600 | 
|---|---|---|
| committer | Kamil Kaminski <kamilkss@gmail.com> | 2011-02-18 19:10:16 -0600 | 
| commit | 2d1eb462fe119d34568e1652b8107fd552c15025 (patch) | |
| tree | 807211ec709b45b2339149adb6fbb5f51d47e569 /bigint/BigIntegerUtils.cc | |
| parent | ec58cdb26cc89df9b9bbb0e33fe843e5b4284955 (diff) | |
| download | rsacrypt-2d1eb462fe119d34568e1652b8107fd552c15025.tar.gz rsacrypt-2d1eb462fe119d34568e1652b8107fd552c15025.tar.bz2 rsacrypt-2d1eb462fe119d34568e1652b8107fd552c15025.zip  | |
begin crypt
Diffstat (limited to 'bigint/BigIntegerUtils.cc')
| -rw-r--r-- | bigint/BigIntegerUtils.cc | 50 | 
1 files changed, 50 insertions, 0 deletions
diff --git a/bigint/BigIntegerUtils.cc b/bigint/BigIntegerUtils.cc new file mode 100644 index 0000000..44073af --- /dev/null +++ b/bigint/BigIntegerUtils.cc @@ -0,0 +1,50 @@ +#include "BigIntegerUtils.hh" +#include "BigUnsignedInABase.hh" + +std::string bigUnsignedToString(const BigUnsigned &x) { +	return std::string(BigUnsignedInABase(x, 10)); +} + +std::string bigIntegerToString(const BigInteger &x) { +	return (x.getSign() == BigInteger::negative) +		? (std::string("-") + bigUnsignedToString(x.getMagnitude())) +		: (bigUnsignedToString(x.getMagnitude())); +} + +BigUnsigned stringToBigUnsigned(const std::string &s) { +	return BigUnsigned(BigUnsignedInABase(s, 10)); +} + +BigInteger stringToBigInteger(const std::string &s) { +	// Recognize a sign followed by a BigUnsigned. +	return (s[0] == '-') ? BigInteger(stringToBigUnsigned(s.substr(1, s.length() - 1)), BigInteger::negative) +		: (s[0] == '+') ? BigInteger(stringToBigUnsigned(s.substr(1, s.length() - 1))) +		: BigInteger(stringToBigUnsigned(s)); +} + +std::ostream &operator <<(std::ostream &os, const BigUnsigned &x) { +	BigUnsignedInABase::Base base; +	long osFlags = os.flags(); +	if (osFlags & os.dec) +		base = 10; +	else if (osFlags & os.hex) { +		base = 16; +		if (osFlags & os.showbase) +			os << "0x"; +	} else if (osFlags & os.oct) { +		base = 8; +		if (osFlags & os.showbase) +			os << '0'; +	} else +		throw "std::ostream << BigUnsigned: Could not determine the desired base from output-stream flags"; +	std::string s = std::string(BigUnsignedInABase(x, base)); +	os << s; +	return os; +} + +std::ostream &operator <<(std::ostream &os, const BigInteger &x) { +	if (x.getSign() == BigInteger::negative) +		os << '-'; +	os << x.getMagnitude(); +	return os; +}  | 
