14,741,685 members
Articles » Web Development » Applications & Tools » General
Article
Posted 4 Jun 2007

86.2K views
17 bookmarked

# Conversion between Binary, Octal, Decimal, & Hexadecimal number systems

Rate me:
4 Jun 2007

## Introduction

Have you ever needed to convert between various number systems. There is no library (built-in) function in C for displaying a number in binary or parsing (reading) an octal number into it's integer equivalent. In this article I am going to introduce some functions I have written for use with this purpose.

## Background

Many time I needed to show a number in binary or parse a binary string into it's equivalent string. Also for other number system I found no standard generalized way. So, I decided to write down my own codes. These are simple in fact. But can surely save time when you don't want to waste time in just writing down the code for displaying a number in binary (or in other formats).

## Using the code

I have written 8 functions for parsing and formatting from and into all the number system representations into others. 4 for parsing 4 number system string representations into their integer equivalents and 4 for formatting strings from integer equivalents. Any combinations can be used for conversion between string representation of one number system to another.

The parsing functions are:-

`int parseBin(char* bin);`

`int parseOct(char* oct);`

`int parseDec(char* dec);`

`int parseHex(char* hex);`

The formatting functions are:-

`char* formatBin(int bin);`

`char* formatOct(int oct);`

`char* formatDec(int dec); `

`char* formatHex(int hex); `

I am discussing two of them here in detail.

`int parseBin(char* bin)` parses binary string bin into it's integer equivalent. To parse the binary number string "1001" into it's equivalent integer-

`printf("%d\n", parseBin("1001"); `

`char* formatHex(int hex)` formats integer hex into it's hexadecimal string representation. To fromat the number 255 into it's hexadecimal string representation-

`printf("%s\n", formatHex(255)); `

Any combination of the functions can be used for possible convertions between string representation of various number systems.

`printf(formatBin(parseHex("3f8")); `

`formatHex()` gives output in uppercase. To have the output in lower case there is another function; `formatHexL()`.

## Points of Interest

I have made an application that can convert between any number system in the command line. The source is included in the zip. The application is named conv_type.

Example: conv_type 0 3 1111

Output: FF

Discussion: conv_type input_system output_system number

0 = binary

1 = octal

2 = decimal

## History

A list of licenses authors might use can be found here

## Share

Linux+Windows programmer.

Mainly focused on networking and web development.

 First Prev Next
 My vote of 1 Andrew Muliar18-Feb-09 20:24 Andrew Muliar 18-Feb-09 20:24
 Article should be removed. fresi15-Feb-09 19:58 fresi 15-Feb-09 19:58
 Re: Article should be removed. Andrew Muliar18-Feb-09 20:23 Andrew Muliar 18-Feb-09 20:23
 Incorrect David Crow4-Aug-08 17:39 David Crow 4-Aug-08 17:39
 malloc Mihai Nita4-Jun-07 10:55 Mihai Nita 4-Jun-07 10:55
 All but binary is there Mihai Nita4-Jun-07 10:53 Mihai Nita 4-Jun-07 10:53
 Re: All but binary is there Mihai Nita4-Jun-07 11:01 Mihai Nita 4-Jun-07 11:01
 Re: All but binary is there [modified] asadbd5-Jun-07 5:24 asadbd 5-Jun-07 5:24
 Last Visit: 31-Dec-99 19:00     Last Update: 26-Jan-21 3:17 Refresh 1