Hallo, im C-Code für einen Atmel Mega328P muss ich einen String auf einen PSTR() und einen String zu einen String zusammensetzten. Gibt es dafür bereits eine elegante Prozedur? Bisher mache ich das so ...
1 | // ...
|
2 | // const char *data
|
3 | // const char text[6]
|
4 | // ...
|
5 | |
6 | // Textlängen
|
7 | uint8_t count_data = string_len_P(data); |
8 | uint8_t count_text = string_len(text); |
9 | uint8_t count_str = count_data + count_text; |
10 | |
11 | // String zusammensetzen
|
12 | char str[count_str+1]; |
13 | |
14 | uint8_t cnt = 0; |
15 | |
16 | for(uint8_t i=0;i<count_data;i++) |
17 | {
|
18 | str[cnt++] = pgm_read_byte(data++); |
19 | }
|
20 | |
21 | for(uint8_t i=0;i<count_text;i++) |
22 | {
|
23 | str[cnt++] = text[i]; |
24 | }
|
25 | |
26 | str[cnt] = '\0'; |
27 | |
28 | // ...
|
29 | |
30 | uint8_t string_len(const char *str) |
31 | {
|
32 | uint8_t i = 0, |
33 | j = 0; |
34 | |
35 | while(str[i++]!='\0') |
36 | {
|
37 | j++; |
38 | }
|
39 | |
40 | return j; |
41 | }
|
42 | |
43 | uint8_t string_len_P(const char *data) |
44 | {
|
45 | uint8_t i = 0; |
46 | |
47 | while(pgm_read_byte(data++)) |
48 | {
|
49 | i++; |
50 | }
|
51 | |
52 | return i; |
53 | }
|
danke Gruß hal