fixed a bug with even numbers of digits in BCD conversion

git-svn-id: https://hamlib.svn.sourceforge.net/svnroot/hamlib/trunk@1207 7ae35d74-ebe9-4afe-98af-79ac388436b8
This commit is contained in:
Stéphane Fillod, F8CFE 2002-10-07 21:57:17 +00:00
parent 440ebc0c99
commit ee248adb53
2 changed files with 25 additions and 21 deletions

View File

@ -2,7 +2,7 @@
* Hamlib Interface - toolbox
* Copyright (c) 2000-2002 by Stephane Fillod and Frank Singleton
*
* $Id: misc.c,v 1.21 2002-08-26 21:26:06 fillods Exp $
* $Id: misc.c,v 1.22 2002-10-07 21:57:16 fillods Exp $
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
@ -97,12 +97,13 @@ void dump_hex(const unsigned char ptr[], size_t size)
* Hope the compiler will do a good job optimizing it (esp. w/ the 64bit freq)
*/
unsigned char *
to_bcd(unsigned char bcd_data[], unsigned long long freq, int bcd_len)
to_bcd(unsigned char bcd_data[], unsigned long long freq, unsigned bcd_len)
{
int i;
unsigned char a;
/* '450'-> 0,5;4,0 */
/* '450'/4-> 5,0;0,4 */
/* '450'/3-> 5,0;x,4 */
for (i=0; i < bcd_len/2; i++) {
a = freq%10;
@ -125,7 +126,7 @@ to_bcd(unsigned char bcd_data[], unsigned long long freq, int bcd_len)
*
* Hope the compiler will do a good job optimizing it (esp. w/ the 64bit freq)
*/
unsigned long long from_bcd(const unsigned char bcd_data[], int bcd_len)
unsigned long long from_bcd(const unsigned char bcd_data[], unsigned bcd_len)
{
int i;
freq_t f = 0;
@ -147,13 +148,19 @@ unsigned long long from_bcd(const unsigned char bcd_data[], int bcd_len)
* Same as to_bcd, but in Big Endian mode
*/
unsigned char *
to_bcd_be(unsigned char bcd_data[], unsigned long long freq, int bcd_len)
to_bcd_be(unsigned char bcd_data[], unsigned long long freq, unsigned bcd_len)
{
int i;
unsigned char a;
/* '450'-> 0,4;5,0 */
/* '450'/4 -> 0,4;5,0 */
/* '450'/3 -> 4,5;0,x */
if (bcd_len&1) {
bcd_data[bcd_len/2] &= 0x0f;
bcd_data[bcd_len/2] |= (freq%10)<<4; /* NB: low nibble is left uncleared */
freq /= 10;
}
for (i=(bcd_len/2)-1; i >= 0; i--) {
a = freq%10;
freq /= 10;
@ -161,10 +168,6 @@ to_bcd_be(unsigned char bcd_data[], unsigned long long freq, int bcd_len)
freq /= 10;
bcd_data[i] = a;
}
if (bcd_len&1) {
bcd_data[0] &= 0xf0;
bcd_data[0] |= freq%10; /* NB: high nibble is left uncleared */
}
return bcd_data;
}
@ -172,21 +175,22 @@ to_bcd_be(unsigned char bcd_data[], unsigned long long freq, int bcd_len)
/*
* Same as from_bcd, but in Big Endian mode
*/
unsigned long long from_bcd_be(const unsigned char bcd_data[], int bcd_len)
unsigned long long from_bcd_be(const unsigned char bcd_data[], unsigned bcd_len)
{
int i;
freq_t f = 0;
if (bcd_len&1)
f = bcd_data[0] & 0x0f;
for (i=bcd_len&1; i < (bcd_len+1)/2; i++) {
for (i=0; i < bcd_len/2; i++) {
f *= 10;
f += bcd_data[i]>>4;
f *= 10;
f += bcd_data[i] & 0x0f;
}
if (bcd_len&1) {
f *= 10;
f += bcd_data[bcd_len/2]>>4;
}
return f;
}

View File

@ -2,7 +2,7 @@
* Hamlib Interface - toolbox header
* Copyright (c) 2000,2001 by Stephane Fillod and Frank Singleton
*
* $Id: misc.h,v 1.14 2002-08-26 21:26:06 fillods Exp $
* $Id: misc.h,v 1.15 2002-10-07 21:57:17 fillods Exp $
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
@ -51,14 +51,14 @@ void dump_hex(const unsigned char ptr[], size_t size);
* reprensentation, and return it.
* bcd_len is the number of digits in the BCD array.
*/
extern HAMLIB_EXPORT(unsigned char *) to_bcd(unsigned char bcd_data[], unsigned long long freq, int bcd_len);
extern HAMLIB_EXPORT(unsigned long long) from_bcd(const unsigned char bcd_data[], int bcd_len);
extern HAMLIB_EXPORT(unsigned char *) to_bcd(unsigned char bcd_data[], unsigned long long freq, unsigned bcd_len);
extern HAMLIB_EXPORT(unsigned long long) from_bcd(const unsigned char bcd_data[], unsigned bcd_len);
/*
* same as to_bcd and from_bcd, but in Big Endian mode
*/
extern HAMLIB_EXPORT(unsigned char *) to_bcd_be(unsigned char bcd_data[], unsigned long long freq, int bcd_len);
extern HAMLIB_EXPORT(unsigned long long) from_bcd_be(const unsigned char bcd_data[], int bcd_len);
extern HAMLIB_EXPORT(unsigned char *) to_bcd_be(unsigned char bcd_data[], unsigned long long freq, unsigned bcd_len);
extern HAMLIB_EXPORT(unsigned long long) from_bcd_be(const unsigned char bcd_data[], unsigned bcd_len);
extern HAMLIB_EXPORT(int) sprintf_freq(char *str, freq_t);
extern HAMLIB_EXPORT(int) sprintf_mode(char *str, rmode_t);