Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

BCD

Posted on 2002-06-13
11
Medium Priority
?
279 Views
Last Modified: 2013-12-03
Does anyone know of a BCD (Binary coded decimal) library available for C?
0
Comment
Question by:philsmicronet
[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
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7075616
Hmm, I cannot see the need for a lib, as BCD is pretty straight foward:

The Binary Coded Decimal, or BCD code, is used as the direct code to communicate decimal numbers using
a binary code. Recall that the decimal numbers are 0 to 9.  BCD code is used to directly translate these
decimal numbers using a 4-bit code.
Here are the decimal equivalent 4-bit BCD codes;
 

Decimal BCD
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001

 
Notice that each single decimal digit has an equivalent 4-bit BCD code.

Converting Decimal to BCD

This conversion simply requires you to represent each decimal digit with its 4-bit BCD code.

Eg.  38210 = (310 = 0011BCD), (810 = 1000BCD) and (210 = 0010BCD)  so;
       the result is 001110000010BCD   and dropping the leading zeros = 1110000010BCD

Converting BCD to Decimal

This is just grouping every 4 bits of BDC code (starting at the LSD) and representing each 4-bit code
with its equivalent decimal number.

Eg.  100100101000BCD = (1000 = 8), (0010 = 2) and (1001 = 9)   so;

        the decimal equivalent is 92810
 


Nevertheless, check out this one: http://www.1cplusplusstreet.com/vb/scripts/ShowCode.asp?txtCodeId=625&lngWId=3 ("BCD math")
0
 
LVL 1

Author Comment

by:philsmicronet
ID: 7076777
What about if you need to add, subtract, multiply etv etc
0
 
LVL 86

Expert Comment

by:jkr
ID: 7076783
That is why I provided the link :o)
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 1

Author Comment

by:philsmicronet
ID: 7076885
This allows you to convert from BCD to double and back, however, if you do this you are going to still have the same rounding problems that you would normally have with floating point arithmetic. I would have thought that generic BCD add, subtract etc etc would be required.
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7077536
"BCD math library C source" on Goolge gives you scores of links.
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 7080533
@ robert_marquardt:
True, google gives you scores of links -- but 'quantity' is spelled differently from 'quality'.

@ philsmicronet:
Basically, any x86-processor supports BCD's. I didn't find a link with a BCD math library. However, this might be useful to you:
http://www.intel.com/design/intarch/techinfo/Pentium/instsum.htm
Search for "Packed BCD Adjustment Instructions"/"Unpacked BCD Adjustment Instructions" -- it deals with double/single-digit BCD's only, but can easily extended to multi-digit-BCD's. It is 16-bit code so don't expect any high-performance-operations. Anyway, here are some ASM-code-snippets:

Addition [using aaa = "ascii adjust after addition"]:
    mov ax,9        ;ax = 0x0009
    mov bx,3        ;bx = 0x0003
    add al,bl       ;ax = 0x000c = 12d
    aaa             ;ax = 0x0102

Multiply [using aam = "ascii adjust after multiply"]:
    mov al,5        ;al = 0x0005
    mov bl,7        ;bl = 0x0007
    mul bl          ;ax = al *  bl = 0x0023 = 35d
    aam             ;ax = 0x0305

Hope that provides at least some help to you. Regards,
.f
0
 
LVL 3

Accepted Solution

by:
jimbucci earned 400 total points
ID: 7126696
Sounds like a Data Structures I project I did many years ago.  The code is in Pascal, but I was able to add and subtract BCD numbers.

1st of all - is this homework?

If not, and if I can find the code, would you be interested in it?

Jim
0
 
LVL 1

Author Comment

by:philsmicronet
ID: 7126762
No, its just aprogram I am working on.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9715016
Wow, that was really a quick accept - so, now, would you be so kind and explain why you accepted an answer that reads " if I can find the code, would you be interested in it?" over the others that had good suggestions? As for my part, this is not really motivating to help you in the future.
0
 
LVL 3

Expert Comment

by:jimbucci
ID: 9715086
I'm puzzled as well.  Must have been a mouse click error.  
Jim
0
 
LVL 1

Author Comment

by:philsmicronet
ID: 9717577
Oooops. Was going through my old questions that I had not accepted (only two) and selected the wrong comment.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

618 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