5bd23d751f
which we already assume)
54 lines
1013 B
C
54 lines
1013 B
C
#include <ctype.h>
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include "ucore/string.h"
|
|
|
|
size_t
|
|
uc_ascii2bcd(const char *restrict ascii, unsigned char *restrict bcdout, unsigned char filler)
|
|
{
|
|
unsigned char *bcdp = bcdout;
|
|
int shift = 0;
|
|
|
|
*bcdp = 0;
|
|
|
|
for (; *ascii; ascii++) {
|
|
if (*ascii < '0' || *ascii > '9') {
|
|
continue;
|
|
}
|
|
|
|
if (shift) {
|
|
*bcdp |= (*ascii - '0') << 4;
|
|
bcdp++;
|
|
} else {
|
|
*bcdp = (*ascii - '0');
|
|
}
|
|
|
|
shift = !shift;
|
|
}
|
|
|
|
if (shift) {
|
|
*bcdp |= (filler & 0xf) << 4;
|
|
bcdp++;
|
|
}
|
|
|
|
return bcdp - bcdout;
|
|
}
|
|
|
|
static const char hex[] = "0123456789ABCDEF";
|
|
size_t
|
|
uc_bcd2ascii(const unsigned char *restrict bcd, size_t bcdlen, char *restrict asciiout)
|
|
{
|
|
size_t i;
|
|
size_t idx = 0;
|
|
|
|
for (i = 0; i < bcdlen; i++) {
|
|
asciiout[idx++] = hex[bcd[i] & 0xf];
|
|
asciiout[idx++] = hex[(bcd[i] & 0xf0) >> 4];
|
|
}
|
|
|
|
asciiout[idx] = 0;
|
|
|
|
return idx;
|
|
}
|
|
|