forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kernel: lib: Add convert functions for hex strings and binary arrays
Move duplicate hex2bin and add bin2hex function so that application can use the functions and avoid code duplication. Signed-off-by: Joakim Andersson <[email protected]>
- Loading branch information
1 parent
743f3db
commit 7a93e94
Showing
9 changed files
with
161 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ zephyr_sources( | |
crc8_sw.c | ||
crc7_sw.c | ||
fdtable.c | ||
hex.c | ||
mempool.c | ||
rb.c | ||
thread_entry.c | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright (c) 2019 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <stddef.h> | ||
#include <zephyr/types.h> | ||
#include <errno.h> | ||
#include <sys/util.h> | ||
|
||
int char2hex(char c, u8_t *x) | ||
{ | ||
if (c >= '0' && c <= '9') { | ||
*x = c - '0'; | ||
} else if (c >= 'a' && c <= 'f') { | ||
*x = c - 'a' + 10; | ||
} else if (c >= 'A' && c <= 'F') { | ||
*x = c - 'A' + 10; | ||
} else { | ||
return -EINVAL; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int hex2char(u8_t x, char *c) | ||
{ | ||
if (x <= 9) { | ||
*c = x + '0'; | ||
} else if (x >= 10 && x <= 15) { | ||
*c = x - 10 + 'a'; | ||
} else { | ||
return -EINVAL; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
size_t bin2hex(const u8_t *buf, size_t buflen, char *hex, size_t hexlen) | ||
{ | ||
if ((hexlen + 1) < buflen * 2) { | ||
return 0; | ||
} | ||
|
||
for (size_t i = 0; i < buflen; i++) { | ||
if (hex2char(buf[i] >> 4, &hex[2 * i]) < 0) { | ||
return 0; | ||
} | ||
if (hex2char(buf[i] & 0xf, &hex[2 * i + 1]) < 0) { | ||
return 0; | ||
} | ||
} | ||
|
||
hex[2 * buflen] = '\0'; | ||
return 2 * buflen; | ||
} | ||
|
||
size_t hex2bin(const char *hex, size_t hexlen, u8_t *buf, size_t buflen) | ||
{ | ||
u8_t dec; | ||
|
||
if (buflen < hexlen / 2 + hexlen % 2) { | ||
return 0; | ||
} | ||
|
||
/* if hexlen is uneven, insert leading zero nibble */ | ||
if (hexlen % 2) { | ||
if (char2hex(hex[0], &dec) < 0) { | ||
return 0; | ||
} | ||
buf[0] = dec; | ||
hex++; | ||
buf++; | ||
} | ||
|
||
/* regular hex conversion */ | ||
for (size_t i = 0; i < hexlen / 2; i++) { | ||
if (char2hex(hex[2 * i], &dec) < 0) { | ||
return 0; | ||
} | ||
buf[i] = dec << 4; | ||
|
||
if (char2hex(hex[2 * i + 1], &dec) < 0) { | ||
return 0; | ||
} | ||
buf[i] += dec; | ||
} | ||
|
||
return hexlen / 2 + hexlen % 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.