added hex_to_bytes

This commit is contained in:
merlokk 2019-12-01 01:22:05 +02:00
commit e88f4e4cd8
2 changed files with 42 additions and 0 deletions

View file

@ -394,6 +394,47 @@ void print_blocks(uint32_t *data, size_t len) {
}
}
int hex_to_bytes(const char *hexValue, uint8_t *bytesValue, size_t maxBytesValueLen) {
char buf[3];
int indx = 0;
int bytesValueLen = 0;
while (hexValue[indx]) {
if (hexValue[indx] == '\t' || hexValue[indx] == ' ') {
indx++;
continue;
}
if (isxdigit(hexValue[indx])) {
buf[strlen(buf) + 1] = 0x00;
buf[strlen(buf)] = hexValue[indx];
} else {
// if we have symbols other than spaces and hex
return -1;
}
if (maxBytesValueLen && bytesValueLen >= maxBytesValueLen) {
// if we dont have space in buffer and have symbols to translate
return -2;
}
if (strlen(buf) >= 2) {
uint32_t temp = 0;
sscanf(buf, "%x", &temp);
bytesValue[bytesValueLen] = (uint8_t)(temp & 0xff);
buf[0] = 0;
bytesValueLen++;
}
indx++;
}
if (strlen(buf) > 0)
//error when not completed hex bytes
return -3;
return bytesValueLen;
}
// takes a number (uint64_t) and creates a binarray in dest.
void num_to_bytebits(uint64_t n, size_t len, uint8_t *dest) {
while (len--) {

View file

@ -55,6 +55,7 @@ char *sprint_ascii_ex(const uint8_t *data, const size_t len, const size_t min_st
void print_blocks(uint32_t *data, size_t len);
int hex_to_bytes(const char *hexValue, uint8_t *bytesValue, size_t maxBytesValueLen);
void num_to_bytebits(uint64_t n, size_t len, uint8_t *dest);
void num_to_bytebitsLSBF(uint64_t n, size_t len, uint8_t *dest);
uint8_t *SwapEndian64(const uint8_t *src, const size_t len, const uint8_t blockSize);