-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathbcdl.c
111 lines (88 loc) · 2.05 KB
/
bcdl.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
/*-----------------01-10-95 02:38pm-----------------
-- BCD conversion routines, released to the public
-- domain by Jeff Dunlop
--------------------------------------------------*/
#include "sniptype.h" /* For True_, False_ */
#include "snipmath.h"
static int BCDLen = 8;
void SetBCDLen(int n)
{
BCDLen = n;
}
long BCDtoLong(char *BCDNum)
{
int i, high, low;
Boolean_T neg;
long total = 0;
for (i = 0; i < BCDLen - 1; i++)
{
low = 0x0f & BCDNum[i];
high = (0xf0 & BCDNum[i]) >> 4;
total *= 10;
total += high;
total *= 10;
total += low;
}
high = (BCDNum[BCDLen - 1] & 0xf0) >> 4;
low = BCDNum[BCDLen - 1] & 0x0f;
total *= 10;
total += high;
neg = (low == 0x0c) ? 1 : -1;
return total * neg;
}
void LongtoBCD(long num, char BCDNum[])
{
int i, high, low;
Boolean_T neg = False_;
if ( num < 0 )
{
neg = True_;
num = 0 - num;
}
low = neg ? 0x0d : 0x0c;
high = (int)(num % 10);
num /= 10;
BCDNum[BCDLen - 1] = (char)((high << 4) | low);
for (i = BCDLen - 2; i >= 0; i--)
{
low = (int)(num % 10);
num /= 10;
high = (int)(num % 10);
num /= 10;
BCDNum[i] = (char)((high << 4) | low);
}
}
#ifdef TEST
#include <stdio.h>
void show_BCD(char c[]);
int main(void)
{
long a = 12345678L,
b;
char c[10];
SetBCDLen(10);
LongtoBCD(a, c);
show_BCD(c);
b= BCDtoLong(c);
printf("Value is %ld\n", b);
a = -a;
LongtoBCD(a, c);
show_BCD(c);
b= BCDtoLong(c);
printf("Value is %ld\n", b);
return 0;
}
void show_BCD(char c[])
{
int i;
printf("BCD: ");
for (i = 0; i < 10; ++i)
{
printf("[%d]", c[i] >> 4);
if (9 == i)
printf("<sign>");
printf("[%d]", c[i] &0xf);
}
puts("");
}
#endif /* TEST */