blob: 98338853ba95e91c67cca6d48166c561d7d39993 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include "relay_types.h"
#include <stdio.h>
#include <string.h>
int relay_hex_to_bytes(const char *hex, size_t hex_len, uint8_t *out, size_t out_len)
{
if (hex_len != out_len * 2) return -1;
for (size_t i = 0; i < out_len; i++) {
unsigned int byte;
if (sscanf(hex + i * 2, "%02x", &byte) != 1) return -1;
out[i] = (uint8_t)byte;
}
return 0;
}
void relay_bytes_to_hex(const uint8_t *bytes, size_t len, char *hex)
{
for (size_t i = 0; i < len; i++)
sprintf(hex + i * 2, "%02x", bytes[i]);
hex[len * 2] = '\0';
}
|