Hello all. I'm writing a program that converts from any cominbation of base 2, 8, 10, and 16. So far I have this code and I want to convert from hexadecimal to octal but it doesn't work. I would appreciate any help. Thank you!
Here is the code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <limits>
#include <sstream>
#include <bitset>
#define ULONG unsigned long
#define FALSE 0
#define TRUE 1
#define BOOL int
#define MAX 1000
using namespace std;
void B2D();
void B2O();
void B2H();
void D2B();
void D2OH();
void O2D();
void O2H();
void H2D();
void H2O();
void H2B();
void binary(int);
int Binaryy;
BOOL oct2dec(ULONG oct, ULONG *dec) {
ULONG digit, result=0, place=1;
while(oct!=0) {
digit = oct % 10;
if(digit>7) { return FALSE; }
oct = oct/10;
result += digit * place;
place *= 8;
}
*dec = result;
return TRUE;
}
void dec2hex(ULONG dec) {
ULONG digit = dec % 16;
if(dec>15) { dec2hex(dec/16); }
printf("%c", (digit>9) ? digit - 10 + 'A' : '0' + digit );
}
void processEntry(ULONG octalValue) {
ULONG decimalValue;
//printf("Value is %ld\n", octalValue);
if (!oct2dec(octalValue, &decimalValue)) {
printf("Sorry out of range..\n");
return;
}
//printf("Decimal Value is %ld\n", decimalValue);
printf("Hexadecimal eq= ");
dec2hex(decimalValue);
printf("\n");
}
int main()
{
char MenuChoice = '1';
while(MenuChoice != '0')
{
cout << "a. Binary to Decimal"<< endl;
cout << "b. Binary to Octal"<< endl;
cout << "c. Binary to Hexadecimal"<< endl;
cout << "d. Decimal to Binary"<< endl;
cout << "e. Decimal to Octal and Hexadecimal"<< endl;
cout << "f. Octal to Decimal"<< endl;
cout << "g. Octal to Hexadecimal"<< endl;
cout << "h. Hexadecimal to Decimal"<< endl;
cout << "i. Hexadecimal to Octal"<< endl;
cout << "j. Hexadecimal to Binary"<< endl;
cout << "0. Quit" << endl;
cout << "Menu Choice: ";
cin >> MenuChoice;
cout << endl;
switch (MenuChoice)
{
case 'a':
B2D();
break;
case 'b':
B2O();
break;
case 'c':
B2H();
break;
case 'd':
D2B();
break;
case 'e':
D2OH();
break;
case 'f':
O2D();
break;
case 'g':
O2H();
break;
case 'h':
H2D();
break;
case 'i':
H2O();
break;
case 'j':
H2B();
break;
case '0':
break;
default:
cout << "Choose only 0-4 please"<< endl;
}
}
return 0;
}
void B2D()
{
cout<< "Binary: "; cin>>Binaryy;
int Digit[11];
int k = 1, i = 0;
while (Binaryy/k != 0)
{
Digit[i] = (Binaryy/k) % 10;
i++;
k *= 10; //k = k*10;
}
int Decimal = 0; i = 0;
while (Digit[i] != -858993460)
{
Decimal += Digit[i]*pow(2,i);
i++;
}
cout<< "Decimal: "<<Decimal<<endl;
}
void B2O()
{
char num_str[50];
cout << "\n Enter the binary value you wish to convert into octal: ";
cin >> num_str;
long num_l = strtol(num_str, NULL, 2);
_ltoa_s (num_l, num_str, 8);
cout << "\n The octal representation of the given binary is: " << num_str << "\n\n";
}
void D2B()
{
int number;
cout << "\n Enter the decimal value you wish to convert into binary: ";
cin >> number;
if (number < 0)
cout << "That is not a positive integer.\n";
else {
cout << number << "\n The binary representation of the given decimal is: ";
binary(number);
cout << endl;
}
}
void binary(int number) {
int remainder;
if(number <= 1) {
cout << number;
return;
}
remainder = number%2;
binary(number >> 1);
cout << remainder;
}
void B2H()
{
long int longint=0;
string buf;
cout<<"\n Enter the binary value you wish to convert into hexadecimal: "<<endl;
cin>>buf;
int len=buf.size();
for(int i=0;i<len;i++)
{
longint+=( buf[len-i-1]-48) * pow(2,i);
}
cout<<setbase(16);
cout<<"\n The hexadecimal representation of the given binary is:";
cout<<longint<<endl<<endl;
}
void D2OH()
{
int n;
cout << "\n Enter the decimal value you wish to convert into octal and hexadecimal: ";
cin >> n;
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
cout << "\n The hexadecimal representation of the given decimal is:" << hex << n << endl;
cout << "\n The octal representation of the given decimal is: " << oct << n << endl;
cout << "Press any key to continue...";
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}
void O2D()
{
int i,OctNum,DecNum = 0,arr[20];
cout<< "\n Enter the octal value you wish to convert into decimal: ";
cin>>OctNum;
for(i=0; OctNum>0; i++)
{
arr[i] = OctNum % 10;
OctNum = OctNum / 10;
}
cout<<i;
for(int power=0, j=0; j<i ; j++,power++)
{
DecNum = DecNum + arr[j] * pow(8.0,power);
}
cout<<"\n\n The decimal representation of the given octal is: "<<DecNum<<endl;
cin.get();
}
void O2H()
{
ULONG octalValue;
char choice = '1';
while(choice=='1') {
printf("\nEnter the octal value you wish to convert into hexadecimal: ");
scanf_s("%ld",&octalValue);
processEntry(octalValue);
printf("Press 1 to enter another value or press 0 to exit\n\n");
printf("Your Choice: ");
scanf_s(" %c",&choice);
}
}
void H2D()
{
int x;
cout << "\nEnter the hexadecimal value you wish to convert into decimal: " << endl<<endl;
cin >> hex >> x;
cout<<endl;
cout <<"\n The decimal representation of the given hexadecimal is: "<< x << endl<<endl;
}
void H2O()
{
int x;
cout << "\nEnter the hexadecimal value you wish to convert into octal: " << endl<<endl;
cin >> oct >> x;
cout<<endl;
cout <<"\n The octal representation of the given hexadecimal is: "<< x << endl<<endl;
}
void H2B()
{
char binNumber[MAX],hexDecimal[MAX];
long int t=0;
cout<<"Enter any hexadecimal number: ";
cin>>hexDecimal;
cout<<" Equivalent binary value: \n";
cout<<"\n";
while(hexDecimal[t]){
switch(hexDecimal[t]){
case '0': cout<<("0000"); break;
case '1': cout<<("0001"); break;
case '2': cout<<("0010"); break;
case '3': cout<<("0011"); break;
case '4': cout<<("0100"); break;
case '5': cout<<("0101"); break;
case '6': cout<<("0110"); break;
case '7': cout<<("0111"); break;
case '8': cout<<("1000"); break;
case '9': cout<<("1001"); break;
case 'A': cout<<("1010"); break;
case 'B': cout<<("1011"); break;
case 'C': cout<<("1100"); break;
case 'D': cout<<("1101"); break;
case 'E': cout<<("1110"); break;
case 'F': cout<<("1111"); break;
case 'a': cout<<("1010"); break;
case 'b': cout<<("1011"); break;
case 'c': cout<<("1100"); break;
case 'd': cout<<("1101"); break;
case 'e': cout<<("1110"); break;
case 'f': cout<<("1111 \n"); break;
default: cout<<"\nInvalid hexadecimal digit %c "<<hexDecimal[t];
}
t++;
}
}
The problem is under void H2O...