1
- /*
1
+ /*
2
2
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
3
- This file is part of the esp8266 core for Arduino environment.
4
-
3
+ This file is part of the esp32 core for Arduino environment.
4
+
5
5
This library is free software; you can redistribute it and/or
6
6
modify it under the terms of the GNU Lesser General Public
7
7
License as published by the Free Software Foundation; either
16
16
License along with this library; if not, write to the Free Software
17
17
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
18
*/
19
+
19
20
#include < Arduino.h>
20
21
#include < HEXBuilder.h>
21
22
@@ -27,43 +28,44 @@ static uint8_t hex_char_to_byte(uint8_t c)
27
28
}
28
29
29
30
size_t HEXBuilder::hex2bytes (unsigned char * out, size_t maxlen, String &in) {
30
- return hex2bytes (out, maxlen, in.c_str ());
31
+ return hex2bytes (out, maxlen, in.c_str ());
31
32
}
32
33
33
34
size_t HEXBuilder::hex2bytes (unsigned char * out, size_t maxlen, const char * in) {
34
- size_t len = 0 ;
35
- for (;*in;in++) {
36
- uint8_t c = hex_char_to_byte (*in);
37
- // Silently skip anything unknown.
38
- if (c > 15 )
39
- continue ;
35
+ size_t len = 0 ;
36
+ for (;*in;in++) {
37
+ uint8_t c = hex_char_to_byte (*in);
38
+ // Silently skip anything unknown.
39
+ if (c > 15 )
40
+ continue ;
40
41
41
- if (len & 1 ) {
42
- if (len/2 < maxlen)
43
- out[len/2 ] |= c;
44
- } else {
45
- if (len/2 < maxlen)
46
- out[len/2 ] = c<<4 ;
47
- }
48
- len++;
49
- }
50
- return (len + 1 )/2 ;
42
+ if (len & 1 ) {
43
+ if (len/2 < maxlen)
44
+ out[len/2 ] |= c;
45
+ } else {
46
+ if (len/2 < maxlen)
47
+ out[len/2 ] = c<<4 ;
48
+ }
49
+ len++;
50
+ }
51
+ return (len + 1 )/2 ;
51
52
}
52
53
53
54
size_t HEXBuilder::bytes2hex (char * out, size_t maxlen, const unsigned char * in, size_t len) {
54
- for (size_t i = 0 ; i < len; i++)
55
- if (i*2 + 1 < maxlen)
56
- sprintf (out + (i * 2 ), " %02x" , in[i]);
57
-
58
- return len * 2 + 1 ;
55
+ for (size_t i = 0 ; i < len; i++) {
56
+ if (i*2 + 1 < maxlen) {
57
+ sprintf (out + (i * 2 ), " %02x" , in[i]);
58
+ }
59
+ }
60
+ return len * 2 + 1 ;
59
61
}
60
62
61
63
String HEXBuilder::bytes2hex (const unsigned char * in, size_t len) {
62
- size_t maxlen = len * 2 + 1 ;
63
- char * out = (char *) malloc (maxlen);
64
- if (!out) return String ();
65
- bytes2hex (out, maxlen, in, len);
66
- String ret = String (out);
67
- free (out);
68
- return ret;
64
+ size_t maxlen = len * 2 + 1 ;
65
+ char * out = (char *) malloc (maxlen);
66
+ if (!out) return String ();
67
+ bytes2hex (out, maxlen, in, len);
68
+ String ret = String (out);
69
+ free (out);
70
+ return ret;
69
71
}
0 commit comments