BCD to Bin

Posted on 1998-09-01
Last Modified: 2008-02-01
How to convert
BIN to BCD ?
Question by:pchaloux
  • 2

Accepted Solution

Answers2000 earned 100 total points
ID: 1252454
Answer in the comment below, this is going to take time to type...

Expert Comment

ID: 1252455
BCD standards for Binary Coded Decimal.

Basically each byte represents to base 10 digits each the highest and lowest 4 bits of the byte

e.g. in binary

AAAA stores a 10s column value (0-9)
BBBB stores a units column value (0-9)

To convert a single byte from BCD to the normal bitwise integer representation

int nTens = ( ( nBCD & 0xF0 ) >> 4 ) * 10 ;
int nUnits = ( nBCD & 0x0F ) ;
int nNormal = ( nTens * 10 ) + nUnits ;

To convert a single byte back from bitwise integers you do the reverse.  However because BCDs take more space, you will need 2 bytes (you need a 100s column too)

int nUnits = (  nValue % 10 ) ;
int nTens = ( nValue / 10 ) % 10 ;
int nHundreds = ( nValue / 100 ) % 10 ;

char hibyte = nHundreds ;
char lobyte = ( nTens << 4 ) | ( nUnits ) ;

Now to expand this to a general case repeat for all columns (for example the 2nd byte of the BCD would contain 100s and 1000s).

A couple of issues you need to be aware of:
1. Where does the sign go ?  Typically your BCD value will have a bit somewhere in it which indicates +ve or -ve.  You may need to flip the sign to account for this
2. Byte ordering, are numbers represented in left to right or right to left order.  Consider one thousand two hundred and thirty four...potentially this may be represented as

00010010 (first byte) - '12' in hex
00110100 (second byte) - '34' in hex


01000011 (first byte) - '43' in hex
00100001 (second byte) - '21' in hex

or (just possible) a couple of variations of this (e.g. first byte might be 34 or 21).  Anyway, you need to take this into accounting in building or extracting your columns

One final point to check are you BCD numbers represented as Exponent/Mantissa.  For example you may find that 12,340 is represented as 2 BCD numbers:
1234 and 1 because 1234 X 10^1 = 12,340

Again you can take this into account my multiplying or dividing by powers of ten.


Author Comment

ID: 1252456
That's exactly what I needed.

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question