File tree Expand file tree Collapse file tree 3 files changed +122
-0
lines changed Expand file tree Collapse file tree 3 files changed +122
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < cstdlib>
3
+ #include " ../libbyteconvert/ByteConvert.hpp"
4
+
5
+ using namespace std ;
6
+
7
+ int main () {
8
+ cout << " Simple test" << endl;
9
+ cout << " size(short): " << sizeof (short ) << endl;
10
+ cout << " size(int): " << sizeof (int ) << endl;
11
+ cout << " size(float): " << sizeof (float ) << endl;
12
+ cout << " size(char): " << sizeof (char ) << endl;
13
+ cout << " size(uint8_t): " << sizeof (uint8_t ) << endl;
14
+ cout << " valToHex: 9->" << ByteConvert::valToHex (0x09 ) << endl;
15
+ cout << " valToHex: b->" << ByteConvert::valToHex (0x0b ) << endl;
16
+ cout << " byteToHexString: fa->" << ByteConvert::byteToHexString (0xfa ) << endl;
17
+
18
+ uint8_t array1[] = {0xfa ,0xca ,0xde ,0xda };
19
+ cout << " facadeda -> " << ByteConvert::arrayToString (4 ,array1) << endl;
20
+
21
+ size_t s2 = 0 ;
22
+ uint8_t *array2 = ByteConvert::stringToArray (s2," bedababa" );
23
+ cout << " Size(bedababba): " << s2 << endl;
24
+ cout << " bedababba -> " << ByteConvert::arrayToString (s2,array2) << endl;
25
+
26
+ delete array2;
27
+
28
+ int in = 5 ;
29
+ cout << " Orig: 5 -> " << in << endl;
30
+ size_t s3 = 0 ;
31
+ uint8_t *array3 = ByteConvert::varToArray<int >(s3,in);
32
+ cout << " 00000005 -> " << ByteConvert::arrayToString (s3,array3) << endl;
33
+ cout << " 5 -> " << ByteConvert::arrayToVar<int >(array3) << endl;
34
+
35
+ delete array3;
36
+
37
+ return 0 ;
38
+ }
Original file line number Diff line number Diff line change
1
+ #include " ByteConvert.hpp"
2
+ namespace ByteConvert {
3
+ char valToHex (uint8_t val) {
4
+ if ((val & 0x0f ) < 10 )
5
+ return (' 0' + val);
6
+ else
7
+ return (' a' + (val - 10 ));
8
+ }
9
+ uint8_t hexToVal (char c) {
10
+ if (c >= ' a' )
11
+ return (15 - (' f' - c));
12
+ else
13
+ return (9 - (' 9' - c));
14
+ }
15
+ std::string byteToHexString (uint8_t b) {
16
+ std::string buffer = " " ;
17
+ buffer += valToHex (b & 0x0f );
18
+ b >>= 4 ;
19
+ buffer = valToHex (b & 0x0f ) + buffer;
20
+ return buffer;
21
+ }
22
+ uint8_t hexStringToByte (std::string block) {
23
+ if (block.length () != 2 )
24
+ return 0x00 ;
25
+ return 16 *hexToVal (block.at (0 ))+hexToVal (block.at (1 ));
26
+ }
27
+
28
+ std::string arrayToString (size_t size,uint8_t *src) {
29
+ std::string buffer = " " ;
30
+ for (size_t i = 0 ;i < (size);i++)
31
+ buffer += byteToHexString (src[i]);
32
+ return buffer;
33
+ }
34
+ uint8_t * stringToArray (size_t &size,std::string src) {
35
+ // Length of src must be odd !!
36
+ if (src.length () % 2 == 1 )
37
+ src = " 0" + src;
38
+ size = src.length ()/2 ;
39
+ uint8_t *dst = new uint8_t [size]; // Allocate memory space
40
+ std::string buff = " " ;
41
+ for (size_t i = 0 ;i < (size);i++) {
42
+ buff = src.at (i*2 );
43
+ buff += src.at (i*2 +1 );
44
+ dst[i] = hexStringToByte (buff);
45
+ }
46
+ return dst;
47
+ }
48
+ }
Original file line number Diff line number Diff line change
1
+ #ifndef _ByteConvert_hpp_
2
+ #define _ByteConvert_hpp_
3
+
4
+ #include < string>
5
+ #include < cstdint>
6
+
7
+ namespace ByteConvert {
8
+ template <class T >
9
+ T arrayToVar (uint8_t *array) {
10
+ size_t size = sizeof (T)/sizeof (uint8_t );
11
+ T var = 0x00 ;
12
+ for (size_t i = 0 ;i < (size);i++) {
13
+ if (i != 0 ) var <<= 8 ;
14
+ var |= array[i];
15
+ }
16
+ return var;
17
+ }
18
+ template <class T >
19
+ uint8_t *varToArray (size_t &size,T var) {
20
+ size = sizeof (T)/sizeof (uint8_t );
21
+ uint8_t *array = new uint8_t [size]; // Alocate memory
22
+ for (size_t i = 0 ;i < size;i++) {
23
+ if (i != 0 ) var >>= 8 ;
24
+ array[size-1 -i] = (uint8_t )(var&0xff );
25
+ }
26
+ return array;
27
+ }
28
+ char valToHex (uint8_t val);
29
+ uint8_t hexToVal (char c);
30
+ std::string byteToHexString (uint8_t b);
31
+ uint8_t hexStringToByte (std::string block);
32
+ std::string arrayToString (size_t size,uint8_t *src);
33
+ uint8_t * stringToArray (size_t &size,std::string src);
34
+ }
35
+
36
+ #endif
You can’t perform that action at this time.
0 commit comments