IoT-MBBR (masterthesis)
Masterthesis upgrading a Moving Bed BioReactor with extra sensors and cloud connectivity.
util_string.h File Reference

String (char array) conversion functionality. More...

#include <stdint.h>
#include <stdbool.h>

Go to the source code of this file.

Functions

void charASCII_to_charHex (char *inbuf, char *outbuf)
 Convert an ASCII char array (string) to a hexadecimal char array. More...
 
void uint32_to_charDec (uint32_t value, char *outbuf)
 Convert a uint32_t value to a decimal char array (string). More...
 
void uint32_to_charHex (uint32_t value, char *outbuf)
 Convert a uint32_t value to a hexadecimal char array (string). More...
 
bool stringStartsWith (char *inbuf, char *sequence)
 Check if a given char array (string) starts with a given sequence. More...
 
uint8_t stringLength (char *inbuf)
 Returns the length of the given null-terminated byte string. More...
 

Detailed Description

String (char array) conversion functionality.

Version
1.0
Author
Brecht Van Eeckhoudt

License

Copyright (C) 2020 - Brecht Van Eeckhoudt

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

A copy of the GNU General Public License can be found in the LICENSE file along with this source code.


Some methods use code obtained from examples from Silicon Labs' GitHub. These sections are licensed under the Silabs License Agreement. See the file "Silabs_License_Agreement.txt" for details. Before using this software for any purpose, you must agree to the terms of that agreement.

Definition in file util_string.h.

Function Documentation

◆ charASCII_to_charHex()

void charASCII_to_charHex ( char *  inbuf,
char *  outbuf 
)

Convert an ASCII char array (string) to a hexadecimal char array.

Note
If the input is not a string (ex.: "Hello world!") but a char array, the input message (array) needs to end with NULL ("\0")!
Parameters
[in]inbufThe given ASCII string to convert to a hexadecimal string.
[out]outbufThe buffer to put the resulting string in.
This needs to have twice the length of the input message + 1! (two HEX characters for one ASCII character + NULL termination)

Definition at line 63 of file util_string.c.

64 {
65  /* Lookup table array */
66  char hex_lut[] = "0123456789abcdef";
67 
68  /* Calculate length of the given string */
69  uint8_t length = stringLength(inbuf);
70 
71  /* Convert each character */
72  for (uint16_t i = 0; i < length; i++)
73  {
74  /* Only get necessary nibble by ANDing with a mask and
75  * shifting one nibble (4 bits) per position */
76  outbuf[(i*2)] = hex_lut[(inbuf[i] & 0xF0) >> 4 ];
77  outbuf[(i*2)+1] = hex_lut[(inbuf[i] & 0x0F) ];
78  }
79 
80  outbuf[length*2] = '\0'; /* NULL termination character */
81 }
uint8_t stringLength(char *inbuf)
Returns the length of the given null-terminated byte string.
Definition: util_string.c:224
unsigned short uint16_t
Typedef defining 16 bit unsigned short type. The developer should modify this to suit the platform be...
Definition: vl53l1_types.h:88
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98

◆ stringLength()

uint8_t stringLength ( char *  inbuf)

Returns the length of the given null-terminated byte string.

Parameters
[in]inbufThe given char array (string).

Definition at line 224 of file util_string.c.

225 {
226  uint8_t length = 0;
227 
228  /* Count up until we reach a NULL */
229  while (inbuf[length] != '\0') length++;
230 
231  return (length);
232 }
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98

◆ stringStartsWith()

bool stringStartsWith ( char *  inbuf,
char *  sequence 
)

Check if a given char array (string) starts with a given sequence.

Note
If the input is not a string (ex.: "Hello world!") but a char array, the input message (array) needs to end with NULL ("\0")!
Parameters
[in]inbufThe given char array (string) to check a sequence in.
[in]sequenceThe sequence to check in the given char array.
Returns
  • true - The given sequence is found in the char array.
  • false -The given sequence is not found in the char array.

Definition at line 203 of file util_string.c.

204 {
205  /* Go through each character of "sequence" */
206  for (uint8_t i = 0; i < stringLength(sequence); i++ )
207  {
208  /* Return false if the value at the memory location (pointer, '*') of
209  * (inbuf+i) isn't equal to the value at the memory location of (sequence+i) */
210  if (*(inbuf+i) != *(sequence+i)) return (false);
211  }
212 
213  return (true);
214 }
uint8_t stringLength(char *inbuf)
Returns the length of the given null-terminated byte string.
Definition: util_string.c:224
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98

◆ uint32_to_charDec()

void uint32_to_charDec ( uint32_t  value,
char *  outbuf 
)

Convert a uint32_t value to a decimal char array (string).

Parameters
[in]valueThe uint32_t value to convert to a string.
[out]outbufThe buffer to put the resulting string in.
This needs to have a length of 10: char buf[10];!

Definition at line 95 of file util_string.c.

96 {
97  if (value == 0)
98  {
99  outbuf[0] = '0';
100  outbuf[1] = '\0'; /* NULL termination character */
101  }
102  else
103  {
104  /* MAX uint32_t value = FFFFFFFFh = 4294967295d (10 decimal chars) */
105  char backwardsBuf[10];
106 
107  uint32_t calcval = value;
108  uint8_t length = 0;
109  uint8_t lengthCounter = 0;
110 
111 
112  /* Loop until the value is zero (separate characters 0-9) and calculate length */
113  while (calcval)
114  {
115  uint32_t rem = calcval % 10;
116  backwardsBuf[length] = '0' + rem; /* Convert to ASCII character */
117  length++;
118 
119  calcval = calcval - rem;
120  calcval = calcval / 10;
121  }
122 
123  /* Backwards counter */
124  lengthCounter = length;
125 
126  /* Reverse the characters in the buffer for the final string */
127  for (uint8_t i = 0; i < length; i++)
128  {
129  outbuf[i] = backwardsBuf[lengthCounter-1];
130  lengthCounter--;
131  }
132 
133  /* Add NULL termination character */
134  outbuf[length] = '\0';
135  }
136 }
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98
unsigned int uint32_t
Typedef defining 32 bit unsigned int type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:78

◆ uint32_to_charHex()

void uint32_to_charHex ( uint32_t  value,
char *  outbuf 
)

Convert a uint32_t value to a hexadecimal char array (string).

Parameters
[in]valueThe uint32_t value to convert to a string.
[out]outbufThe buffer to put the resulting string in.
This needs to have a length of 9: char buf[9];!

Definition at line 150 of file util_string.c.

151 {
152  /* Lookup table array */
153  char hex_lut[] = "0123456789abcdef";
154 
155  /* 4 nibble HEX representation */
156  if (value <= 0xFFFF)
157  {
158  /* Only get necessary nibble by ANDing with a mask and
159  * shifting one nibble (4 bits) per position */
160  outbuf[0] = hex_lut[(value & 0xF000) >> 12];
161  outbuf[1] = hex_lut[(value & 0x0F00) >> 8 ];
162  outbuf[2] = hex_lut[(value & 0x00F0) >> 4 ];
163  outbuf[3] = hex_lut[(value & 0x000F) ];
164  outbuf[4] = '\0'; /* NULL termination character */
165  }
166 
167  /* 8 nibble HEX representation */
168  else
169  {
170  /* Only get necessary nibble by ANDing with a mask and
171  * shifting one nibble (4 bits) per position */
172  outbuf[0] = hex_lut[(value & 0xF0000000) >> 28];
173  outbuf[1] = hex_lut[(value & 0x0F000000) >> 24];
174  outbuf[2] = hex_lut[(value & 0x00F00000) >> 20];
175  outbuf[3] = hex_lut[(value & 0x000F0000) >> 16];
176  outbuf[4] = hex_lut[(value & 0x0000F000) >> 12];
177  outbuf[5] = hex_lut[(value & 0x00000F00) >> 8 ];
178  outbuf[6] = hex_lut[(value & 0x000000F0) >> 4 ];
179  outbuf[7] = hex_lut[(value & 0x0000000F) ];
180  outbuf[8] = '\0'; /* NULL termination character */
181  }
182 }