Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

This resource has not currently been approved, and is not currently linked to from our directory of resources. It is being displayed here for preview by the author and moderators only.
Rated
Read 258 times

Related Categories

[C++] Number Base Conversion (binary to octal) - Descriptions & Code

Descriptions & Code

===============<  Descriptions   >============

-> VSC++ 2005 - Win32 Console App
-> User input a number in binary form, we will print that in octal base. (It's a simple conversion between number bases and will inform you about some helpful C++ standard functions. You can see more infos about the functions in MSDN.)

===============<  Code   >============

#include <iostream> // system()
using namespace std;
 
#include <cstdlib> // strtol(), ltoa()
 
 
int main()
{
     
      char num_str[50];
 
      cout << "\n Enter a number in binary base, for converting to octal : ";
      cin >> num_str;
 
      long num_l = strtol(num_str, NULL, 2); // conversion : binary(char*) -> decimal(long)
     
      ltoa (num_l, num_str, 8); // conversion : decimal(long) -> octal(char*)
     
      cout << "\n Number in octal base : " << num_str << "\n\n";
 
      system("pause");
      return 0;
}

Comments