R
Robbie Hatley
For your enjoyment, a function that expresses any integer with
absolute value less-than-or-equal-to nine quintillion in any
base from 2 to 36. (For larger bases, you could expand the
"digit" character string with, perhaps, foreign letters with
diacritical marks, if you don't mind iso-8895-1 instead of ASCII.
And if you don't like the non-std type "long long" you can always
change it to "long"; but then it could only handle numbers up to
about 2 billion, instead of 9 quintillion.
///////////////////////////////////////////////////////////////////////////
// //
// Base //
// Represent an integer in any base from 2 to 36. //
// //
///////////////////////////////////////////////////////////////////////////
std::string
Base
(
int base, // must be >= 2 and <= 36
int precision, // must be >= 1 and <= 63
long long number, // must be >= -9E18 and <= +9E18
bool leading_zeros // does user want leading zeros?
)
{
const long long MAX = 9223372036854775807LL;
const long long MIN = -9223372036854775808LL;
double largest = pow(base, precision) - 1;
if
(
base < 2 || base > 36 // If base is out-of-range
|| precision < 1 || precision > 63 // or precision is out-of-range
|| number < MIN || number > MAX // or number is out-of-range
|| largest > MAX // or base/precision combo is out-of-range
|| largest < number // or base/precision combo can't express number
)
{
return std::string("ERROR"); // then return "ERROR".
}
std:stringstream repre;
if (number < 0)
{
number *= (-1);
repre << '-';
}
long long place = 1LL;
for (int i = 1; i <= precision - 1; ++i)
{
place *= base;
}
long long value = 0LL;
std::string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
bool first_non_zero = false;
for ( ; place > 0; place /= base)
{
value = number / place;
if (value > 0) first_non_zero = true;
if (leading_zeros || first_non_zero) repre << digits[value];
number -= value * place;
}
return repre.str();
}
--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
absolute value less-than-or-equal-to nine quintillion in any
base from 2 to 36. (For larger bases, you could expand the
"digit" character string with, perhaps, foreign letters with
diacritical marks, if you don't mind iso-8895-1 instead of ASCII.
And if you don't like the non-std type "long long" you can always
change it to "long"; but then it could only handle numbers up to
about 2 billion, instead of 9 quintillion.
///////////////////////////////////////////////////////////////////////////
// //
// Base //
// Represent an integer in any base from 2 to 36. //
// //
///////////////////////////////////////////////////////////////////////////
std::string
Base
(
int base, // must be >= 2 and <= 36
int precision, // must be >= 1 and <= 63
long long number, // must be >= -9E18 and <= +9E18
bool leading_zeros // does user want leading zeros?
)
{
const long long MAX = 9223372036854775807LL;
const long long MIN = -9223372036854775808LL;
double largest = pow(base, precision) - 1;
if
(
base < 2 || base > 36 // If base is out-of-range
|| precision < 1 || precision > 63 // or precision is out-of-range
|| number < MIN || number > MAX // or number is out-of-range
|| largest > MAX // or base/precision combo is out-of-range
|| largest < number // or base/precision combo can't express number
)
{
return std::string("ERROR"); // then return "ERROR".
}
std:stringstream repre;
if (number < 0)
{
number *= (-1);
repre << '-';
}
long long place = 1LL;
for (int i = 1; i <= precision - 1; ++i)
{
place *= base;
}
long long value = 0LL;
std::string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
bool first_non_zero = false;
for ( ; place > 0; place /= base)
{
value = number / place;
if (value > 0) first_non_zero = true;
if (leading_zeros || first_non_zero) repre << digits[value];
number -= value * place;
}
return repre.str();
}
--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/