Advertisement
Fernando_Fiore

BaseConverter

Aug 6th, 2020
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. // BaseConverter.h: interface for the CBaseConverter class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #if !defined(AFX_BASECONVERTER_H__28149E41_2127_11D7_A179_000103DD18CD__INCLUDED_)
  6. #define AFX_BASECONVERTER_H__28149E41_2127_11D7_A179_000103DD18CD__INCLUDED_
  7.  
  8. #if _MSC_VER > 1000
  9. #pragma once
  10. #endif // _MSC_VER > 1000
  11. # include <string>
  12. # include <cassert>
  13. # include <algorithm>
  14. # include <cmath>
  15. using std::string;
  16.  
  17. namespace NO5
  18. {
  19.     namespace Math
  20.     {
  21.         class CBaseConverter  
  22.         {
  23.             // returns -1 if its not a valid character
  24.             static int GetDecValue(char c)
  25.             {
  26.                 int res = -1;
  27.  
  28.                 if( c >= '0' && c <= '9'){
  29.                     res = (int(c) - int('0'));
  30.                 }
  31.                 else if(c >= 'A' && c <= 'Z'){
  32.                     res = (int(c) - int('A'));
  33.                     res += 10;
  34.                 }
  35.                 else if(c >= 'a' && c <= 'z'){
  36.                     res = (int(c) - int('a'));
  37.                     res += 10;
  38.                 }
  39.                 return res;
  40.             }
  41.             static char GetBaseXDigit(unsigned int i,bool UpCase =\
  42.                 true)
  43.             {
  44.                 char res;
  45.  
  46.                 if(i >= 0 && i <= 9){
  47.                     res = char(int('0') + i);
  48.                 }
  49.                 else if(i < 36){
  50.                     if(UpCase){
  51.                         res = char(int('A') + i - 10);
  52.                     }
  53.                     else{
  54.                         res = char(int('a') + i - 10);
  55.                     }
  56.                 }
  57.                 else{
  58.                     assert(0);
  59.                     // throw something;
  60.                 }
  61.                 return res;
  62.             }
  63.             static bool IsBaseXDigit(char c,unsigned int b)
  64.             {
  65.                 int dec = GetDecValue(c);
  66.                
  67.                 return (dec > 0 && dec < b);
  68.             }
  69.  
  70.         public:
  71.             // returns -1 if value is not valid
  72.             static int FromBaseX(const string &value,unsigned\
  73.                 int b)
  74.             {
  75.                 int res = 0;
  76.                 const string::size_type len = value.length();
  77.                 string::size_type i;
  78.                 int tmp;
  79.  
  80.                 for(i=0;i<len;i++){
  81.                     tmp = GetDecValue(value[len - i - 1]);
  82.                     if(tmp < 0){
  83.                         res = -1;
  84.                         break;
  85.                     }
  86.                     res += tmp * ( pow(b,i));
  87.                 }
  88.                 return res;
  89.             }
  90.             static string ToBaseX(unsigned int value,unsigned int b,
  91.                 bool UpCase = true)
  92.             {
  93.                 unsigned int q,r;
  94.                 string res;
  95.  
  96.                 q = value;
  97.                 while(q != 0){
  98.                     r = q % b;
  99.                     q = q / b;
  100.                     res += GetBaseXDigit(r,UpCase);
  101.                 }
  102.                 std::reverse(res.begin(),res.end());
  103.                 return res;
  104.             }
  105.             static string BaseXtoY(const string &value,unsigned int\
  106.                 b_x,unsigned int b_y,bool bUpCase = true)
  107.             {
  108.                 string res;
  109.  
  110.                 if(b_x == b_y)
  111.                     res = value;
  112.                 else
  113.                     res = ToBaseX(FromBaseX(value,b_x),b_y,bUpCase);
  114.                 return res;
  115.             }
  116.         };
  117.     }
  118. }
  119.  
  120. #endif // !defined(AFX_BASECONVERTER_H__28149E41_2127_11D7_A179_000103DD18CD__INCLUDED_)
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement