-
Notifications
You must be signed in to change notification settings - Fork 1
/
strings.cpp
80 lines (70 loc) · 1.78 KB
/
strings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "strings.h"
int FLOAT_N = sizeof(float);
int LONG_N = sizeof(long);
//private function for float/char converion
unsigned char charToHexDigit(char c)
{
if (c >= 'A')
return c - 'A' + 10;
else
return c - '0';
}
//private function for float.char conversion
unsigned char stringToByte(char c[3])
{
return charToHexDigit(c[0]) * 16 + charToHexDigit(c[1]);
}
//Converts a float val to hex string
void floatToChar(char* cOut, float fIn){
FloatChar fc;
char hex_buf[3];
cOut[0] = '\0';
fc.fVal = fIn;
for (int i = 0; i < FLOAT_N; i++) {
sprintf(hex_buf, "%02X", fc.cVal[i]);
strcat(cOut, hex_buf);
}
}
//Converts a long val to hex string
void longToChar(char* cOut, unsigned long lIn){
LongChar lc;
char hex_buf[3];
cOut[0] = '\0';
lc.lVal = lIn;
for (int i = LONG_N - 1; i >= 0; i--) {
sprintf(hex_buf, "%02X", lc.cVal[i]);
strcat(cOut, hex_buf);
}
}
//converts a hex string back to float
float charToFloat(char* cIn){
FloatChar fc;
char hex_buf[3];
int c = 0;
for (int i = 0; i < FLOAT_N; i++){
substring(cIn, hex_buf, c, c + 1);
c+=2;
fc.cVal[i] = stringToByte(hex_buf);
}
return fc.fVal;
}
//pads strings with empty spaces, mainly for lcd display.
void padstring(char* str, int len) {
int startIdx = strlen(str) - 1;
for (int i = startIdx; i < len; i++) {
str[i] = ' ';
}
++str = '\0';
}
//gives a substring of a string, based on a start and end index.
void substring(char* inStr, char* outStr, int startIdx, int endIdx){
for (int i = startIdx; i <= endIdx; i++) {
outStr[i - startIdx] = inStr[i];
}
++outStr = '\0';
}
//adds a char to a string.
void strcharcat(char* str, char value){
str[strlen(str)] = value;
++str = '\0';
}