35 bool is_negative =
false;
46 uint8_t digit = num % 10;
49 *str++ = (char)(digit +
'0');
70 bool negative =
false;
71 uint8_t num_array[12] = { 0 };
72 uint8_t digit_counter = 0;
81 num_array[digit_counter] = *str - 48;
87 uint32_t multiplier = 1;
88 for(int8_t i = digit_counter - 1; i >= 0 ; i--) {
89 num += num_array[i] * multiplier;
105 uint8_t int_part_len;
106 uint8_t frac_part_len;
107 uint8_t total_len = 0;
109 uint8_t missing_zeroes = 0;
110 bool negative_0_to_1 =
false;
112 if(num > -1 && num < 0) {
113 negative_0_to_1 =
true;
116 int_part = (int32_t)num;
117 frac_part = ((num - (double)int_part) *
get_pow(10, frac_digits));
119 addend = 1.0 / (20 *
get_pow(10, frac_digits));
129 for(uint8_t i = 1; i <= frac_digits; i++) {
130 int32_t comp_to_frac = (
get_pow(10, frac_digits - i));
132 if(frac_part < comp_to_frac) {
139 total_len += missing_zeroes;
141 char int_buf[20] = { 0 };
142 if(negative_0_to_1) {
150 total_len += int_part_len;
152 char double_buf[12] = { 0 };
155 total_len += frac_part_len;
157 int_buf[int_part_len] =
'.';
160 for(uint8_t i = 0; i < missing_zeroes; i++) {
161 int_buf[int_part_len + 1 + i] =
'0';
164 str_set(int_buf, double_buf, frac_part_len, total_len - frac_part_len);
166 int_buf[total_len] =
'\0';
169 for(uint8_t i = 0; i < total_len; i++) {
176 uint8_t first_part =
byte / 16;
177 uint8_t second_part =
byte % 16;
179 if(first_part < 10) {
180 str[0] = (
byte / 16) + 48;
183 str[0] = (
byte / 16) + 55;
186 if(second_part < 10) {
187 str[1] = (
byte % 16) + 48;
190 str[1] = (
byte % 16) + 55;
195void str_set(
char *target_str,
char const *host_str, uint32_t host_str_len, uint32_t pos) {
196 uint32_t pos_counter = 0;
197 bool pos_reached =
false;
198 uint32_t bytes_replaced = 0;
200 while(bytes_replaced != host_str_len) {
201 if(pos_counter == pos) {
206 *target_str = host_str[bytes_replaced];
209 if(bytes_replaced == host_str_len) {
220int32_t
get_pow(int32_t base, int32_t exponent) {
223 for(int32_t i = 0; i < exponent; i++) {
234 for(uint8_t read = 0; read < len; read++) {
235 if(arr[read] !=
'\0') {
236 arr[write] = arr[read];
248bool str_cmp(
const char *str1,
const char *str2) {
249 while(*str1 !=
'\0' && *str2 !=
'\0') {
257 if(*str1 ==
'\0' && *str2 ==
'\0') {
269 if (high >=
'0' && high <=
'9')
270 nibble = (uint8_t)(high -
'0');
272 nibble = (uint8_t)(high -
'A' + 10);
274 value = (uint8_t)(nibble << 4);
276 if (low >=
'0' && low <=
'9')
277 nibble = (uint8_t)(low -
'0');
279 nibble = (uint8_t)(low -
'A' + 10);
287int str_tokenize(
char *str,
const char *separator, uint16_t max_tokens,
char **tokens, uint16_t *num_tokens) {
297 if(*num_tokens <= max_tokens) {
298 tokens[*num_tokens] = str;
299 *num_tokens = *num_tokens + 1;
305 while(*str && *str != *separator) {
321 if(
str_cmp(str,
"true") ==
true) {
324 else if(
str_cmp(str,
"false") ==
true) {
333int str_cpy(
char *str_to,
const char *str_from, uint32_t len)
335 if (len == 0 || str_to == (
void*)0 || str_from == (
void*)0) {
339 while (*str_from && len > 1) {
340 *str_to++ = *str_from++;
350int txt_cpy(
char *txt_to,
const char *txt_from, uint32_t len) {
351 if (len == 0 || txt_to == (
void*)0 || txt_from == (
void*)0) {
355 while (*txt_from && len != 0) {
356 *txt_to++ = *txt_from++;
365 return num && !(num & (num - 1));
369uint32_t
extract_bits(
const uint8_t *data, uint16_t start_bit, uint8_t num_bits) {
371 uint16_t byte_index = start_bit / 8;
372 uint8_t bit_offset = start_bit % 8;
374 for (
int i = 0; i < num_bits; i++) {
376 if (data[byte_index] & (1 << (7 - bit_offset))) {
377 result |= (1 << (num_bits - 1 - i));
381 if (bit_offset >= 8) {
Common utility module public API.
bool str_cmp(const char *str1, const char *str2)
Compares two null-terminated strings for equality.
int str_to_int(const char *str)
Converts a decimal string to an integer.
int str_cpy(char *str_to, const char *str_from, uint32_t len)
Copies a null-terminated string into a destination buffer.
uint32_t extract_bits(const uint8_t *data, uint16_t start_bit, uint8_t num_bits)
Extracts a range of bits from a big-endian byte array.
void hex_byte_to_str(uint8_t byte, char *str)
Converts a single byte to a two-character uppercase hexadecimal string.
void str_set(char *target_str, char const *host_str, uint32_t host_str_len, uint32_t pos)
Overwrites a region of a target string with the contents of a source string.
uint8_t ascii_hex_to_byte(char high, char low)
Converts two ASCII hex characters into a single byte value.
uint32_t get_str_len(char const *str)
Returns the length of a string, excluding the null terminator.
void double_to_str(double num, char *str, int8_t frac_digits)
Converts a double to a decimal string with a fixed number of fractional digits.
bool str_to_bool(char const *str)
Converts a string representation of a boolean to a bool value.
bool is_pow(uint32_t num)
Checks whether a number is a power of two.
int str_tokenize(char *str, const char *separator, uint16_t max_tokens, char **tokens, uint16_t *num_tokens)
Splits a string into tokens separated by a given delimiter.
int txt_cpy(char *txt_to, const char *txt_from, uint32_t len)
Copies a fixed-length block of text into a destination buffer.
void int_to_str(int num, char *str)
Converts an integer to its decimal string representation.
int32_t get_pow(int32_t base, int32_t exponent)
Computes an integer power.
void arr_cmprs(char *arr, uint8_t len)
Compresses an array by removing null bytes and shifting remaining elements left.