Hello guys, I have a task that requires convertation between hecademical to 8th 10th and binary. And I face a problem that A = 10; B = 11 and so on. I cannot use integer and as far as i used string i cant really use string since if I try to make mathematical action it takes ASCII number instead of number[i]. Could you give me some ideas or hints?
I take it, that the user will be asked, "What is the original number in? ( Binary, Octal, Decimal, or Hexidecimal)".
Then they tell what to convert that number, into.
#include <iostream>
int main () {
int n = 0;
std::cout << "hex ";
std::cin >> std::hex >> n;
// whatever the user did type, was read as a hex value
return 0;
}
# include <iostream>
# include <cmath>
int main (int, char **) {
unsigned N = 10; /* Input value. */
/* Radix of the output. */
constexpr uint64_t radix = 2;
// When radix > 9 you'll not see your additional symbols because there's no
// logic for printing more than 0-9. For instance, 0xA will echo as 10 -- referring to the
// eleventh symbol in your alphabet. If you want, you can use an array or map
// to perform the conversion.
//
// Extra points for getting this to work for negative and fractional
// values. More extra points for doing the math to figure out when IEEE754
// binary64 precision problems causes the conversion to be inexact.
for (int place = ceil (log(N) / log(radix)); place >= 0; place--)
std:: cout << static_cast <uint64_t> (N / pow(radix, place)) % radix
<< " ";
}
And how should i cope with it if I need to transform float numbers as well? I tried to make the div and mod parts two different integers but couldnt use % function since its only suitable for integers.