BCD to Bin

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

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

734 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