Skip to content

About

Console-based decimal to binary and binary to decimal converter in C++

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Number Base Converter

A C++ console program that converts numbers between decimal, binary, hexadecimal, and octal.

What it does

The program shows a menu with these options:

  1. Convert decimal to binary
  2. Convert binary to decimal
  3. Convert hexadecimal to binary
  4. Convert binary to hexadecimal
  5. Convert octal to binary
  6. Convert binary to octal
  7. Exit

You type in a number and it prints the converted value. It keeps looping until you choose exit.

How it works

decimalToBinary repeatedly divides by 2 and builds the binary result digit by digit, using increasing place values (1, 10, 100, ...) so each bit lands in the right spot of the output integer.

binaryToDecimal reads the binary number one digit at a time from the right, multiplying each digit by the correct power of 2 and adding it to a running total.

hexadecimalToBinary and octalToBinary convert each digit of the input to its fixed-width binary equivalent (4 bits per hex digit, 3 bits per octal digit), then strip any leading zeros from the result.

binaryToHexadecimal and binaryToOctal group the binary string into chunks from the right (4 bits for hex, 3 bits for octal), convert each chunk to its digit, then reverse the result into the correct order.

Build and run

g++ -o converter code.cpp
./converter

On Windows with MinGW:

g++ -o converter.exe code.cpp
converter.exe

Notes

Hex input is capped at 8 digits. Binary and decimal results are stored as plain ints, so this works for numbers small enough that the binary representation still fits in an int when read as base 10 digits.

About

Console-based decimal to binary and binary to decimal converter in C++

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages