Click here to Skip to main content
Click here to Skip to main content

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

By , 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

3 = hexadecimal

History

This article is in it's first edition.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

asadbd
Bangladesh Bangladesh
Member
Linux+Windows programmer.
 
Mainly focused on networking and web development.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberAndrew Muliar18 Feb '09 - 19:24 
There is built-in C functions for that.
GeneralArticle should be removed.memberfresi15 Feb '09 - 18:58 
Article should be removed. .
GeneralRe: Article should be removed.memberAndrew Muliar18 Feb '09 - 19:23 
Agree.
But it is also reviewer fault.
GeneralIncorrectmvpDavidCrow4 Aug '08 - 16:39 
asadbd wrote:
There is no library (built-in) function in C for...parsing (reading) an octal number into it's integer equivalent.

 
You can use strtol(..., 8) for this.
 

"Love people and use things, not love things and use people." - Unknown

"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch


GeneralmallocmemberMihai Nita4 Jun '07 - 9:55 
When your API returns an allocated buffer, you have to specify what API is used do do the allocation (so that the user knows what to use to free the memory).
And returning allocated memory across modules is dangerous (if your API will be in a DLL).
 
Just good coding practice.
AnswerRe: mallocmemberete.asad5 Jun '07 - 6:45 
yep. thanks.
 
Be a man-
Write your device drivers yourself

GeneralAll but binary is therememberMihai Nita4 Jun '07 - 9:53 
You can just use the standard library: sprintf and sscanf with %o, %d, %x

GeneralRe: All but binary is therememberMihai Nita4 Jun '07 - 10:01 
My bad, even conversion from binary is there: strtoul( string, NULL, 2 );
GeneralRe: All but binary is there [modified]memberete.asad5 Jun '07 - 4:24 
strtoul may be used for parsing binary string but what about formatting a number into binary string representation? Anyway, I just wanted a easy-to-use, common and clean way for all type of conversion. But I also appreciate your reply.
 

-- modified at 10:34 Tuesday 5th June, 2007
 
Be a man-
Write your device drivers yourself

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 4 Jun 2007
Article Copyright 2007 by asadbd
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid