-
Notifications
You must be signed in to change notification settings - Fork 45
/
bcdd.c
229 lines (197 loc) · 9.8 KB
/
bcdd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*FILE*****************************************************************/
/* File Id: bcd.c. */
/* Author: Stan Milam. */
/* Date Written: 31-Jan-95. */
/* Description: */
/* Routines to manage binary coded decimal. */
/* */
/* bcd_to_double() - Convert BCD to type double value. */
/* double_to_bcd() - Convert double type value to BCD. */
/* */
/* Placed in the public domain by the author, 8-Sep-95 */
/* */
/*****************************************************************FILE*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include "snipmath.h"
/*FUNCTION*************************************************************/
/* Name: bcd_to_double(). */
/* */
/* Description: */
/* This function will convert buffer containing binary coded dec- */
/* imal to a double. */
/* */
/* Arguments: */
/* void *buf - Address of buffer containing binary coded */
/* decimal number. */
/* size_t buflen- Length of the buffer. */
/* int digits- The number of digits to the right of the */
/* decimal point. */
/* */
/* Return Value: */
/* A value of type double which should be the equivalent of the */
/* BCD value. */
/* */
/**********************************************************************/
double bcd_to_double(void *buf, size_t len, int digits)
{
double rv = 0.0;
char *buffer = (char *) buf;
size_t high, low, index, max = len - 1;
digits = abs(digits);
/******************************************************************/
/* Loop through the buffer repeatedly extracting the high and low */
/* 4 bits from each byte and calculating the return value. */
/******************************************************************/
for (index = 0; index < max; index++)
{
low = buffer[index] & 0x0f;
high = (buffer[index] & 0xf0) >> 4;
rv = (( rv * 10.0 + high ) * 10.0 + low);
}
/******************************************************************/
/* The first byte of the buffer contains the lowest order digit */
/* in the upper 4 bits and the sign in the lower 4 bits. */
/******************************************************************/
low = buffer[max] & 0x0f;
high = (buffer[max] & 0xf0) >> 4;
rv = rv * 10.0 + high;
if (digits > 0)
rv /= pow(10, digits);
if (low == 0x0d)
rv = -rv;
return rv;
}
/*FUNCTION*************************************************************/
/* Name: double_to_bcd(). */
/* */
/* Description: */
/* This function will convert a value of type double to a legit- */
/* mate Binary Coded Decimal (BCD) value. */
/* */
/* Arguments: */
/* double arg - The value to be converted. */
/* char *buf - The buffer where the BCD value is stored. */
/* size_t length - The number of significant digits to store in */
/* BCD buffer. */
/* size_t digits - The number of digits to the right of the */
/* decimal point to be stored in the BCD */
/* buffer. */
/* */
/* Return Value: */
/* An integer value indicating the length of the BCD value in the */
/* buffer. -1 is returned if an error occured. */
/* */
/*************************************************************FUNCTION*/
int double_to_bcd(double arg, char *buf, size_t length, size_t digits )
{
char wrkbuf[50], format[DBL_DIG + 1];
int y_sub, x_sub, rv, negative=0;
/******************************************************************/
/* Do a couple of sanity checks first. */
/******************************************************************/
if ((length == 0 && digits == 0) || (length + digits > DBL_DIG))
rv = -1;
else
{
/**************************************************************/
/* If the double argument is negative make a note of it and */
/* con- vert the value to be positive. */
/**************************************************************/
if (arg < 0.0)
{
arg = -arg;
negative = 1;
}
/**************************************************************/
/* Adjust for decimal digits. */
/**************************************************************/
if (digits > 0)
{
length += digits;
arg *= pow( 10, digits );
}
/**************************************************************/
/* Build the format string, build the string and compute the */
/* return value. */
/**************************************************************/
sprintf( format, "%%0%d.0f", length );
sprintf( wrkbuf, format, floor( arg ) );
if ((rv = (length / 2 ) + (length / 2 != 0)) == 0)
rv = 1;
/**************************************************************/
/* Compute the subscript values and clear the BCD buffer. */
/**************************************************************/
y_sub = rv - 1;
x_sub = strlen( wrkbuf ) - 1;
memset( buf, 0, y_sub + 1 );
/**************************************************************/
/* Plug in the sign bits and first BCD digit. */
/**************************************************************/
buf[y_sub] = negative == 1 ? 0xd : 0xc;
buf[y_sub--] |= ( ( wrkbuf[x_sub--] - '0' ) << 4 );
/**************************************************************/
/* While we have more digits to plug.... */
/**************************************************************/
while ( --length > 0 )
{
/**********************************************************/
/* Do the low nibble of the BCD byte. */
/**********************************************************/
buf[y_sub] = wrkbuf[x_sub--] - '0';
if (--length <= 0)
break;
/**********************************************************/
/* Now do the high nibble. */
/**********************************************************/
buf[y_sub--] |= ((wrkbuf[x_sub--] - '0') << 4);
}
}
return rv;
}
/**/
#ifdef TEST
typedef struct {
double value, expect;
int length, digits;
} TEST_T;
int main(void)
{
double rv, value;
char wrkbf[25];
int rc, x_sub, y_sub, size, len, digits;
char format[] = " %10.3f %d %d %d ";
TEST_T testvals[] = {
{ 12345.67, 123.45, 5, 0 },
{ 12345.67, 12345.0, 5, 1 },
{ 12345.67, 12345.67, 4, 3 },
{ 12345.678, 2345.67, 1, 2 },
{ -12345.00, -12345.00, 8, 2 },
{ -1234.56, -12345.00, 5, 3 },
{ 1234.567, 0.60, 1, 3 }
};
size = sizeof( testvals ) / sizeof( TEST_T );
printf(" Double Length Digits Return\n");
printf(" Argument Argument Argument Value Buffer\n");
printf(" ================================================="
"=========\n");
for (x_sub = 0; x_sub < size; x_sub++)
{
len = testvals[x_sub].length;
digits = testvals[x_sub].digits;
value = testvals[x_sub].value;
rc = double_to_bcd(value, wrkbf, len, digits);
if (rc > 0)
{
printf( format, value, len, digits, rc );
for ( y_sub = 0; y_sub < rc; y_sub++ )
printf("%02X ", wrkbf[y_sub]);
printf("\n");
}
}
return 0;
}
#endif