fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int mpow(int a,int b){
  7. auto r=1;
  8. for (auto i = 0;i<b;i++){
  9. r*=a;
  10. }
  11. return r;
  12. }
  13.  
  14. string dec2nth(int k,int n){
  15. int j = log(k+0.5)/log(n);
  16. j++;
  17. auto z = 0;
  18. string result = "";
  19. for (int i = j-1;i>=0;i--){
  20. z = 0;
  21. while(mpow(n,i)<=k){
  22. k -= mpow(n,i);
  23. z++;
  24. }
  25. result += to_string(z);
  26. }
  27. return result;
  28. }
  29. int nth2dec(string k,int n){
  30. auto j = k.length();
  31. auto result = 0;
  32. for (int i=j-1;i>=0;i--){
  33. result += atoi(k.substr(j-1-i,1).c_str())*mpow(n,i);
  34. }
  35. return result;
  36. }
  37.  
  38. string mth2nth(string k,int m,int n){
  39. int result1 = nth2dec(k,m);\
  40. return dec2nth(result1,n);
  41. }
  42. string bin2hex(string k){
  43. int prom;
  44. string res = "";
  45. for (int i = 0;i<ceil(k.length()/4);i++){
  46. prom = nth2dec(k.substr(4*i,4),2);
  47. if (prom < 10){
  48. res += to_string(prom);
  49. }
  50. else {
  51. res += char(55+prom);
  52. }
  53. }
  54. return res;
  55. }
  56.  
  57. string nth2hex(string k,int l){
  58. string res1 = mth2nth(k,l,2);
  59. return bin2hex(res1);
  60. }
  61.  
  62. string hex2nth(string k,int l){
  63. auto j = k.length();
  64. auto result = 0;
  65. auto q = 0;
  66. for (int i=j-1;i>=0;i--){
  67. if (r<10){
  68. q = atoi(k.substr(j-1-i,1).c_str());
  69. }
  70. else {
  71. q = (int)k.substr(j-1-i,1).c_str()[0] - 55;
  72. }
  73. result += atoi(q)*mpow(16,i);
  74. }
  75. return mth2nth(to_string(result),10,l);
  76. }
  77.  
  78. int main() {
  79. //cout << mth2nth("",2,8);
  80. cout << hex2nth("FFF",10);
  81. return 0;
  82. }
Compilation error #stdin compilation error #stdout 0s 3300KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘std::string hex2nth(std::string, int)’:
prog.cpp:67:7: error: ‘r’ was not declared in this scope
   if (r<10){
       ^
prog.cpp:73:19: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]
   result += atoi(q)*mpow(16,i);
                   ^
In file included from /usr/include/features.h:371:0,
                 from /usr/include/i386-linux-gnu/c++/4.8/bits/os_defines.h:39,
                 from /usr/include/i386-linux-gnu/c++/4.8/bits/c++config.h:426,
                 from /usr/include/c++/4.8/iostream:38,
                 from prog.cpp:1:
/usr/include/stdlib.h:278:1: error:   initializing argument 1 of ‘int atoi(const char*)’ [-fpermissive]
 __NTH (atoi (const char *__nptr))
 ^
stdout
Standard output is empty