diff --git a/nv/nv.c b/nv/nv.c index a7b2a60..8145a65 100644 --- a/nv/nv.c +++ b/nv/nv.c @@ -49,30 +49,15 @@ void nv_debug(void) } #endif -int nv_str2hex(char* str, unsigned char* out, unsigned int* outlen) +int nv_str2hex(char* str, uint8_t* hex, uint32_t hex_len) { - char* p = str; - char high = 0, low = 0; - int tmplen = strlen(p), cnt = 0; - while (cnt < (tmplen / 2)) { - high = ((*p > '9') && ((*p <= 'F') || (*p <= 'f'))) ? *p - 48 - 7 - : *p - 48; - low = (*(++p) > '9' && ((*p <= 'F') || (*p <= 'f'))) ? *(p)-48 - 7 - : *(p)-48; - out[cnt] = ((high & 0x0f) << 4 | (low & 0x0f)); - p++; - cnt++; + for (int i = 0; i < hex_len; i++) { + int number; + sscanf(str + i * 2, "%02x", &number); + hex[i] = number; } - if (tmplen % 2 != 0) { - out[cnt] = ((*p > '9') && ((*p <= 'F') || (*p <= 'f'))) ? *p - 48 - 7 - : *p - 48; - } - - if (outlen != NULL) { - *outlen = tmplen / 2 + tmplen % 2; - } - return tmplen / 2 + tmplen % 2; + return 0; } bool nv_read(char* data) diff --git a/nv/nv.h b/nv/nv.h index 1bbe189..a7be7cc 100644 --- a/nv/nv.h +++ b/nv/nv.h @@ -54,7 +54,7 @@ bool nv_read(char* data); nv_t* nv_get(void); -int nv_str2hex(char* str, unsigned char* out, unsigned int* outlen); +int nv_str2hex(char* str, uint8_t* hex, uint32_t hex_len); #if CONFIG_NV_DEBUG_MOCK_DATA void nv_debug(void);