mirror of
https://github.com/Hamlib/Hamlib
synced 2026-08-13 17:32:05 -04:00
to_hex() left non-empty destination buffers untouched when the source
length was zero. Initialize the output string before returning and cover
the empty conversion in testbcd.
(cherry picked from commit 439f91fa28)
75 lines
1.5 KiB
C
75 lines
1.5 KiB
C
|
|
/*
|
|
* Very simple test program to check BCD conversion against some other --SF
|
|
* This is mainly to test freq2bcd and bcd2freq functions.
|
|
*/
|
|
|
|
#include "hamlib/config.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "hamlib/rig.h"
|
|
#include "misc.h"
|
|
|
|
#define MAXDIGITS 32
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
unsigned char b[(MAXDIGITS + 1) / 2];
|
|
char hex[] = "x";
|
|
freq_t f = 0;
|
|
int digits = 10;
|
|
int i;
|
|
|
|
if (argc != 2 && argc != 3)
|
|
{
|
|
fprintf(stderr, "Usage: %s <freq> [digits]\n", argv[0]);
|
|
exit(1);
|
|
}
|
|
|
|
if (to_hex(0, NULL, sizeof(hex), hex) != 0 || hex[0] != '\0')
|
|
{
|
|
fprintf(stderr, "Empty hexadecimal output is not terminated\n");
|
|
exit(1);
|
|
}
|
|
|
|
f = (freq_t)atoll(argv[1]);
|
|
|
|
if (argc > 2)
|
|
{
|
|
digits = atoi(argv[2]);
|
|
|
|
if (digits > MAXDIGITS)
|
|
{
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
printf("Little Endian mode\n");
|
|
printf("Frequency: %"PRIfreq"\n", f);
|
|
to_bcd(b, f, digits);
|
|
printf("BCD: %2.2x", b[0]);
|
|
|
|
for (i = 1; i < (digits + 1) / 2; i++)
|
|
{
|
|
printf(",%2.2x", b[i]);
|
|
}
|
|
|
|
// cppcheck-suppress *
|
|
printf("\nResult after recoding: %"PRIll"\n", (int64_t)from_bcd(b, digits));
|
|
|
|
printf("\nBig Endian mode\n");
|
|
printf("Frequency: %"PRIfreq"\n", f);
|
|
to_bcd_be(b, f, digits);
|
|
printf("BCD: %2.2x", b[0]);
|
|
|
|
for (i = 1; i < (digits + 1) / 2; i++)
|
|
{
|
|
printf(",%2.2x", b[i]);
|
|
}
|
|
|
|
printf("\nResult after recoding: %"PRIll"\n",
|
|
(int64_t)from_bcd_be(b, digits));
|
|
|
|
return 0;
|
|
}
|