473,473 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Fixed BCD code.

Cause I made such a hash of my first attempt, and in an effort to provide
PORTABLE code to solve the problem, I submit this solution to convert to
and from BCD values stored in longs. This should work just as well for
ints and (in theory) chars and, indeed, any integral type. If it doesn't,
suggest ways to improve it, even at a significant speed/code size penalty.

#include <math.h>

long bcd(long decimal)
{
int i;
long result = 0;

for(i = 0; decimal; ++i) {
result += (decimal % 10) * (int) pow(16,i);
decimal /= 10;
}

return(result);
}

long dec(long bcd)
{
long result = 0;
int i;

for(i = 0; bcd; ++i) {
result += (bcd % 16) * (int) pow(10,i);
bcd /= 16;
}

return(result);
}

/* Code tested on all of one machine, but C is portable in theory. */

Nov 13 '05 #1
2 9204
In article <Xn**********************************@63.223.5.101 >,
August Derleth <li*****************@onewest.net> wrote:
Cause I made such a hash of my first attempt, and in an effort to provide
PORTABLE code to solve the problem, I submit this solution to convert to
and from BCD values stored in longs. This should work just as well for
ints and (in theory) chars and, indeed, any integral type. If it doesn't,
suggest ways to improve it, even at a significant speed/code size penalty.

#include <math.h>

long bcd(long decimal)
{
int i;
long result = 0;

for(i = 0; decimal; ++i) {
result += (decimal % 10) * (int) pow(16,i);
pow(16, i) returns a double, which you then cast to an int. There is
no guarantee that either has as much precision as a long. Instead:
result += (decimal %10) << (4 * i);
(Also offers the performance benefit of not converting back and forth
between floating point and integer.)

For similar reasons:

long dec (long bcd)
{
long result = 0, i = 1;

while (bcd) {
result += bcd % 16 * i;
i *= 10;
bcd /= 16;
};
}
/* Code tested on all of one machine, but C is portable in theory. */


Only when you don't make assumptions about implementation-defined
portions of the standard.

-- Brett
Nov 13 '05 #2
rb*@panix.com (Brett Frankenberger) wrote in
news:bj**********@reader2.panix.com on Fri 05 Sep 2003 09:04:00p:
In article <Xn**********************************@63.223.5.101 >,
August Derleth <li*****************@onewest.net> wrote:
for(i = 0; decimal; ++i) { result += (decimal % 10) * (int)
pow(16,i);


pow(16, i) returns a double, which you then cast to an int. There is
no guarantee that either has as much precision as a long. Instead:
result += (decimal %10) << (4 * i);


Ah, but will the shifts be portable to machines that use, say, multiples
of three for their word-size? Will my multiplies?

I can't think of how BCD would work in an octal system (where you'd only
have three bits to encode each digit*), or on a hypothetical ternary
system (which /good/ C code is supposed to be portable to).

Is the problem itself limited to certain kinds of systems?

*Three bits only gives you eight numbers. Nine bits is 8+1, giving you the
possibility of encoding one `traditional' BCD digit with the option of a
sign bit. You can do the math from there.
/* Code tested on all of one machine, but C is portable in theory. */


Only when you don't make assumptions about implementation-defined
portions of the standard.


Touche'.
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Adrian Parker | last post by:
I'm using the code below in my project. When I print all of these fixed length string variables, one per line, they strings in questions do not properly pad with 0s. strQuantity prints as " ...
1
by: Jaz | last post by:
Trying to use a fixed layer for a couple of NAV buttons. I found this code, but the IE part is commented, and I don't understand the IF statement. It works great on Moz/Firebird and Opera BUT...
6
by: Mason A. Clark | last post by:
Masters: On two or three-column layouts, one column often has a list of links. Scrolling the page hides them. I'm aware there's supposed to be the ability to fix the column (frame-like). I...
5
by: Arthur Mnev | last post by:
This is probably beaten to death subject... Does anyone have a good idea of what penalties are for using Fixed statement in c#. On one side it allows for much greater flexibility with casts and...
3
by: Dave | last post by:
I have the following code in one of my programs; ert= CO3; int Code = SR(ert, 1024, 1024); char UnsafeTempBuffer = new char; fixed ( char* tBuffer= UnsafeTempBuffer) { Code =...
2
by: coz | last post by:
I created a wrapper class for a dll written in C. Will the following code prevent the "ENTIRE ARRAY" from being moved, not just the first array element? The Wrapper class will be instantiated in...
3
by: Chris Dunaway | last post by:
I was using .Net Reflector to look at some methods in the String class and found this one: Friend Sub AppendInPlace(ByVal value As Char*, ByVal count As Integer, ByVal currentLength As Integer)...
4
by: taskswap | last post by:
I'm converting an application that relies heavily on a binary network protocol. Within this protocol are a lot of byte arrays of character data, like: public unsafe struct MsgAddEntry {...
0
by: Ken Varn | last post by:
I have a managed C++ assembly in which I need to interact with some 'C' APIs that take fixed size 'C' data blocks. I need to wrap these data blocks into a managed object. It seems like a lot of...
1
by: Rick Knospler | last post by:
I am trying to convert a vb6 project to vb.net. The conversion worked for the most part except for the fixed length strings and fixed length string arrays. Bascially the vb6 programmer stored all...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.