Hello, I am currently unsure on how to convert a binary base to a quantenary base.
Here is a function I have written for decimal to binary conversions but any hints or advice would be helpful, please don't give me the answer.
1 2 3 4 5
for(int i = (sizeof(int) * 8) - 1; i >= 0; i--)
{
unsignedint o = (number >> i) & 01;
os << o;
}
they all work the same way. Your issue is trying to see cute ways to implement, which is important for performance if you have a lot to do, but what is the PROCESS?
in binary... until zero...
take the remainder of 2 and divide by 2.
in base 10, take the remainder of 10 and divide by 10
etc. All the same, but you can optimize the code for each one.
Yours is a conversion from any positive number, not any decimal number, to base 2. The point is that there are no "decimal" numbers. Decimal, binary, quaternary, etc. are just properties of a number's representation.
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include <string>
// assumes nonnegative n, positive b, b <= strlen(digits)
std::string to_base(int n, int b, charconst* digits = "0123456789abcdef")
{ return (n/b? to_base(n/b, b, digits): "") + std::string(1, digits[n%b]); }
int main()
{
std::cout << to_base(17, 4) << '\n'; // convert 17 to base 4, output 101
}