Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nv: update str2hex api #4

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 6 additions & 21 deletions nv/nv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion nv/nv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading