fix hex 2 binstring functions

This commit is contained in:
iceman1001 2023-03-19 12:49:38 +01:00
commit 9399d4e400
2 changed files with 24 additions and 10 deletions

View file

@ -911,27 +911,32 @@ https://github.com/ApertureLabsLtd/RFIDler/blob/master/firmware/Pic32/RFIDler.X/
// convert hex to sequence of 0/1 bit values // convert hex to sequence of 0/1 bit values
// returns number of bits converted // returns number of bits converted
int hextobinarray(char *target, char *source) { int hextobinarray(char *target, char *source) {
int length, i, count = 0; return hextobinarray_n(target, source, strlen(source));
}
int hextobinarray_n(char *target, char *source, int sourcelen) {
int i, count = 0;
char *start = source; char *start = source;
length = strlen(source);
// process 4 bits (1 hex digit) at a time // process 4 bits (1 hex digit) at a time
while (length--) { while (sourcelen--) {
char x = *(source++); char x = *(source++);
// capitalize // capitalize
if (x >= 'a' && x <= 'f') if (x >= 'a' && x <= 'f') {
x -= 32; x -= 32;
}
// convert to numeric value // convert to numeric value
if (x >= '0' && x <= '9') if (x >= '0' && x <= '9') {
x -= '0'; x -= '0';
else if (x >= 'A' && x <= 'F') } else if (x >= 'A' && x <= 'F') {
x -= 'A' - 10; x -= 'A' - 10;
else { } else {
PrintAndLogEx(INFO, "(hextobinarray) discovered unknown character %c %d at idx %d of %s", x, x, (int16_t)(source - start), start); PrintAndLogEx(INFO, "(hextobinarray) discovered unknown character %c %d at idx %d of %s", x, x, (int16_t)(source - start), start);
return 0; return 0;
} }
// output // output
for (i = 0 ; i < 4 ; ++i, ++count) for (i = 0 ; i < 4 ; ++i, ++count) {
*(target++) = (x >> (3 - i)) & 1; *(target++) = (x >> (3 - i)) & 1;
}
} }
return count; return count;
@ -939,9 +944,14 @@ int hextobinarray(char *target, char *source) {
// convert hex to human readable binary string // convert hex to human readable binary string
int hextobinstring(char *target, char *source) { int hextobinstring(char *target, char *source) {
int length = hextobinarray(target, source); return hextobinstring_n(target, source, strlen(source));
if (length == 0) }
int hextobinstring_n(char *target, char *source, int sourcelen) {
int length = hextobinarray_n(target, source, sourcelen);
if (length == 0) {
return 0; return 0;
}
binarraytobinstring(target, target, length); binarraytobinstring(target, target, length);
return length; return length;
} }

View file

@ -112,7 +112,11 @@ int param_getbin_to_eol(const char *line, int paramnum, uint8_t *data, int maxda
int param_getstr(const char *line, int paramnum, char *str, size_t buffersize); int param_getstr(const char *line, int paramnum, char *str, size_t buffersize);
int hextobinarray(char *target, char *source); int hextobinarray(char *target, char *source);
int hextobinarray_n(char *target, char *source, int sourcelen);
int hextobinstring(char *target, char *source); int hextobinstring(char *target, char *source);
int hextobinstring_n(char *target, char *source, int sourcelen);
int binarraytohex(char *target, const size_t targetlen, const char *source, size_t srclen); int binarraytohex(char *target, const size_t targetlen, const char *source, size_t srclen);
void binarraytobinstring(char *target, char *source, int length); void binarraytobinstring(char *target, char *source, int length);
int binstring2binarray(uint8_t *target, char *source, int length); int binstring2binarray(uint8_t *target, char *source, int length);