SingingCat 0
application
strings-common.c
1#include "main-header.h"
2// #include "bare-metal.h"
3#include <stdarg.h>
4
9void bufToHex(char *target, const byte *source, int size) {
10 int i;
11
12 target[0] = 0;
13 for (i = 0; i < size; i++) {
14 snprintf(target + (i * 3), 20, "%x ", source[i]);
15 }
16}
17int charToHex(char c) {
18 if ((c >= '0') && (c <= '9')) {
19 return c - '0';
20 }
21 if ((c >= 'a') && (c <= 'f')) {
22 return c - 'a' + 10;
23 }
24 if ((c >= 'A') && (c <= 'F')) {
25 return c - 'A' + 10;
26 }
27 return -1;
28}
29void ltoh(const long i, char *s) {
30 int x;
31 int y;
32 int o;
33
34 s[0] = 0;
35 o = i;
36 for (x = 7; x >= 0; x--) {
37 y = o & 0xF;
38 if (y < 10) {
39 s[x] = '0' + y;
40 } else {
41 s[x] = 'A' + (y - 10);
42 }
43 o = o >> 4;
44 }
45 s[8] = 0;
46}
47
48// returns >0 if c1 starts with c2
49int startsWith(const char *c1, const char *c2) {
50 int i;
51
52 if ((c1 == NULL) || (c2 == NULL)) {
53 return 0;
54 }
55 if (strlen(c1) < strlen(c2)) {
56 return 0;
57 }
58 for (i = 0; i < strlen(c2); i++) {
59 if (c1[i] != c2[i]) {
60 return 0;
61 }
62 }
63 return 1;
64}
65
66
67void printHex(const char *prefix, const uint8_t *buf, const int size) {
68 int MAX_PER_ROW = 30;
69 int i;
70 int pcr;
71
72 printf("******* hexdump (%i bytes) **** \r\n", size);
73 pcr = 0;
74 for (i = 0; i < size; i++) {
75 printf("%x ", buf[i]);
76 pcr = 0;
77 if ((i != (size - 1)) && ((i % MAX_PER_ROW) == (MAX_PER_ROW - 1))) {
78 pcr = 1;
79 printf("\r\n%s", prefix);
80 }
81 }
82 if (!pcr) {
83 printf("\n");
84 }
85 printf("******* end hexdump\r\n");
86}