Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

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

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

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

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.

Beginning C++23
Kickstart your coding journey with Beginning C++23 – the ultimate guide to mastering the latest in modern C++ programming!
View on Amazon

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

About The Author

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.