C++ Convert a number from base A to base B

mycplus

mycplus

This is a C++ class which has a member function that takes 3 arguments and converts the number from first base to second base. It takes the following arguments.

– std::string thestlstring

– int base

– int base2

The number is stored in the form of std::string and returns the number as string . It supports bases 2 till base 16.

Source code is contributed by: massiveattack92(at)hotmail.com

#include <cmath>
#include <string>
#include <sstream>

#include "inputs.h"

inputs::inputs(std::string newstring, int newbase, int newbase2) {
  inpthenumber = newstring;
  inpbase = newbase;
  inpbase2 = newbase2;
}

inputs::~inputs(void) {}

std::string inputs::conv(std::string thestlstring, int base, int base2) {

  // variable declarations

  int myval;
  int remainder;
  int loopvar = 0;
  int mynumber = 0;
  int testsum = 0;
  float value1;
  float value2;
  char mychar;
  char element;
  std::string stlstring;
  std::stringstream thestream;

  // generate base 10 integer from number in base 'mybase'

  string::iterator pos;
  pos = thestlstring.end();

  for (--pos; pos >= thestlstring.begin(); --pos) {
    mychar = * pos;

    switch (mychar) {
    case '0':
      myval = 0;
      break;
    case '1':
      myval = 1;
      break;
    case '2':
      myval = 2;
      break;
    case '3':
      myval = 3;
      break;
    case '4':
      myval = 4;
      break;
    case '5':
      myval = 5;
      break;
    case '6':
      myval = 6;
      break;
    case '7':
      myval = 7;
      break;
    case '8':
      myval = 8;
      break;
    case '9':
      myval = 9;
      break;
    case 'A':
      myval = 10;
      break;
    case 'B':
      myval = 11;
      break;
    case 'C':
      myval = 12;
      break;
    case 'D':
      myval = 13;
      break;
    case 'E':
      myval = 14;
      break;
    case 'F':
      myval = 15;
      break;
    }
    mynumber += myval * (int) std::pow(base, loopvar);

    ++loopvar;
  }
  // generate 'n' - (n-1) = # of digits in base 10 number
  // generated above
  for (int n = 0; testsum < mynumber; ++n) {
    testsum = testsum + (int) std::pow(base2, n) * (base2 - 1);
  }
  // declare array for insertion of digits from the
  // converted number
  char * revarray = new char[n - 1];

  // generate the new number
  for (int i = 0; i < n; ++i) {
    value1 = (float)((float) mynumber / (float) base2);
    value2 = (float) floor((float) mynumber / (float) base2);
    remainder = mynumber - (int) value2 * base2;

    if (remainder < 10) element = char(remainder + '0');
    else if (remainder == 10) element = 'A';
    else if (remainder == 11) element = 'B';
    else if (remainder == 12) element = 'C';
    else if (remainder == 13) element = 'D';
    else if (remainder == 14) element = 'E';
    else if (remainder == 15) element = 'F';
    // insert an element each time within the loop 
    revarray[i] = element;
    // move down one base multiple 
    mynumber = (mynumber - remainder) / base2;
  }
  // create std::string format of converted number 
  for (int i2 = n - 1; i2 >= 0; --i2) {
    thestream << revarray[i2];
  }
  thestream >> stlstring;
  // return the number
  return stlstring;
}

//----------------------
//inputs.h
//----------------------

#include <string>

class inputs {
  private:

    std::string inpthenumber;
  int inpbase;
  int inpbase2;

  public:
    inputs(std::string, int, int); // called
  // automatically when instance created

  ~inputs(void); // automatically called
  // when object released from RAM
  // must return void.

  std::string conv(std::string thestlstring, int base, int base2);

};

Sample Output

Input: thestlstring = “777”, base = 8, base2 = 10
Output: 511
Explanation: 777 in octal (base =8) when converted to decimal (base =10) is 511.

Input: thestlstring = “927”, base = 10, base2 = 16
Output: 39F
Explanation: 927 in decimal (base =10) when converted to hexadecimal (base = 16) is 39F.

You can verify these values using a scientific calculator program available in windows OS.

M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post