Solved

BCD to Bin

Posted on 1998-09-01
3
2,115 Views
Last Modified: 2008-02-01
How to convert
BCD to BIN
and
BIN to BCD ?
0
Comment
Question by:pchaloux
[X]
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
3 Comments
 
LVL 8

Accepted Solution

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

Expert Comment

by:Answers2000
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
AAAABBBB

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

or

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.

0
 

Author Comment

by:pchaloux
ID: 1252456
That's exactly what I needed.
Thanks
0

Featured Post

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
Grammars for C C++ and java 1 139
Please explain C language error codes ? 5 45
Crashing when deleting value with no child in Binary Tree 4 32
asp.net web app 3 27
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…
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 and use pointers 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.

726 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