Go Premium for a chance to win a PS4. Enter to Win


BCD to Bin

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

Accepted Solution

Answers2000 earned 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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 SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.
Suggested Courses

916 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