Solved

BCD to Bin

Posted on 1998-09-01
3
2,073 Views
Last Modified: 2008-02-01
How to convert
BCD to BIN
and
BIN to BCD ?
0
Comment
Question by:pchaloux
  • 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now