added some helper functions

This commit is contained in:
iceman1001 committed 2023-10-01 13:05:08 +02:00
1 parent 7320f0dd16
commit bb5d61dca7
11 files changed
+111 -28

No files matched your search

+21 -3
View File
@@ -944,11 +944,11 @@ int hextobinarray_n(char *target, char *source, int sourcelen) {
return count;
}
// convert hex to human readable binary string
// convert hexstring to human readable binary string
int hextobinstring(char *target, char *source) {
return hextobinstring_n(target, source, strlen(source));
}
// convert hexstring to human readable binary string
int hextobinstring_n(char *target, char *source, int sourcelen) {
int length = hextobinarray_n(target, source, sourcelen);
if (length == 0) {
@@ -958,6 +958,23 @@ int hextobinstring_n(char *target, char *source, int sourcelen) {
return length;
}
// convert bytes to binary string
int byte_2_binstr(char *target, const uint8_t *source, size_t sourcelen) {
//uint8_t *p = *source;
for (int i = 0 ; i < sourcelen; ++i) {
uint8_t b = *(source++);
*(target++) = ((b >> 7) & 0x1) + '0';
*(target++) = ((b >> 6) & 0x1) + '0';
*(target++) = ((b >> 5) & 0x1) + '0';
*(target++) = ((b >> 4) & 0x1) + '0';
*(target++) = ((b >> 3) & 0x1) + '0';
*(target++) = ((b >> 2) & 0x1) + '0';
*(target++) = ((b >> 1) & 0x1) + '0';
*(target++) = (b & 0x1) + '0';
}
*target = '\0';
}
// convert binary array of 0x00/0x01 values to hex
// return number of bits converted
int binarraytohex(char *target, const size_t targetlen, const char *source, size_t srclen) {
@@ -1007,8 +1024,9 @@ int binarraytohex(char *target, const size_t targetlen, const char *source, size
// convert binary array to human readable binary
void binarraytobinstring(char *target, char *source, int length) {
for (int i = 0 ; i < length; ++i)
for (int i = 0 ; i < length; ++i) {
*(target++) = *(source++) + '0';
}
*target = '\0';
}