找到这篇文章说明你对摩尔斯电码很赶兴趣,而且你已掌握了莫尔斯电码的基础知识了。想更贴近的感受一下莫尔斯电码的魅力。或你有一个非常棒的关于莫 尔斯电码想法而非常激动。如果是这样的话,那你或许会从我的代码中得到帮助,缩短你实现想法征途。如果真的能帮助你的话,那么我也会很高兴。我也是如此的 喜欢莫尔斯电码。
有任何疑问或者有想法想与人一起分享,邮箱我:robert.cysy@gmail.com
我把代码放到了github上了 地址 https://github.com/robert1207/morse_encode.git
可以在Linux下用Makefile 编译,也可以在windows下新建工程添加github上的代码就可以了
下面是实现代码:
main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include < string.h>
4
5 #include " morse.h "
6
7 #define BUF_LEN 300
8
9
10 int main() {
11
12 char *mystr = " abcdefghijklmnopqrstuvwxyz0123456789.:,;?='/!-_\"()$&@ ";
13 char mor[BUF_LEN];
14 char str[BUF_LEN];
15 char out[BUF_LEN];
16 memset( out, 0, BUF_LEN);
17 memset(mor, 0, BUF_LEN);
18 memset(str, 0, BUF_LEN);
19
20 printf( " base string:\n%s\n ", mystr);
21
22 // TO LOWCASE
23 str2lowcase(mystr, out, BUF_LEN);
24
25 // TO MORSE STRING
26 String2MorseString( out , mor, BUF_LEN);
27 printf( " \nget morse code string:\n%s\n " , mor);
28
29
30 // TO NORMAL STRING
31 MorseString2String(mor, str, BUF_LEN);
32 printf( " \nget decode string:\n%s\n ", str);
33
34 return 0;
35 }
2 #include <stdlib.h>
3 #include < string.h>
4
5 #include " morse.h "
6
7 #define BUF_LEN 300
8
9
10 int main() {
11
12 char *mystr = " abcdefghijklmnopqrstuvwxyz0123456789.:,;?='/!-_\"()$&@ ";
13 char mor[BUF_LEN];
14 char str[BUF_LEN];
15 char out[BUF_LEN];
16 memset( out, 0, BUF_LEN);
17 memset(mor, 0, BUF_LEN);
18 memset(str, 0, BUF_LEN);
19
20 printf( " base string:\n%s\n ", mystr);
21
22 // TO LOWCASE
23 str2lowcase(mystr, out, BUF_LEN);
24
25 // TO MORSE STRING
26 String2MorseString( out , mor, BUF_LEN);
27 printf( " \nget morse code string:\n%s\n " , mor);
28
29
30 // TO NORMAL STRING
31 MorseString2String(mor, str, BUF_LEN);
32 printf( " \nget decode string:\n%s\n ", str);
33
34 return 0;
35 }
下面是Morse代码模块:
morse.h
1 #ifndef MORSE_H_
2 #define MORSE_H_
3
4
5 typedef int bool;
6 #define false 0
7 #define true 1
8
9 /*
10 * FAKE_SPACE IS MARING FOR A SPACE
11 */
12 #define FAKE_SPACE '/'
13
14 /*
15 * THE CHARACTER THAT BETWEEN TWO MORSE STRING
16 */
17 #define SEPARATOR ' '
18
19
20 typedef struct Morse Morse_t;
21 struct Morse{
22 char c[ 9];
23 };
24
25
26
27 Morse_t *new_morse();
28
29 bool str2morse( char m , Morse_t *morse);
30 bool morse2str(Morse_t *morse, char *ch);
31
32 bool mark2morse( char n, Morse_t *morse);
33 bool morse2mark(Morse_t *morse, char *n);
34
35 bool num2morse( char n, Morse_t *morse);
36 bool morse2num(Morse_t *morse, char *n);
37
38 void MorseString2String( char *morse , char * string, int buf_len);
39
40 void String2MorseString( char * string , char *morse, int buf_len);
41
42 void str2lowcase( char *str, char * out, int buf_len);
43
44 #endif /* MORSE_H_ */
2 #define MORSE_H_
3
4
5 typedef int bool;
6 #define false 0
7 #define true 1
8
9 /*
10 * FAKE_SPACE IS MARING FOR A SPACE
11 */
12 #define FAKE_SPACE '/'
13
14 /*
15 * THE CHARACTER THAT BETWEEN TWO MORSE STRING
16 */
17 #define SEPARATOR ' '
18
19
20 typedef struct Morse Morse_t;
21 struct Morse{
22 char c[ 9];
23 };
24
25
26
27 Morse_t *new_morse();
28
29 bool str2morse( char m , Morse_t *morse);
30 bool morse2str(Morse_t *morse, char *ch);
31
32 bool mark2morse( char n, Morse_t *morse);
33 bool morse2mark(Morse_t *morse, char *n);
34
35 bool num2morse( char n, Morse_t *morse);
36 bool morse2num(Morse_t *morse, char *n);
37
38 void MorseString2String( char *morse , char * string, int buf_len);
39
40 void String2MorseString( char * string , char *morse, int buf_len);
41
42 void str2lowcase( char *str, char * out, int buf_len);
43
44 #endif /* MORSE_H_ */
morse.c
1 #include
"
morse.h
"
2
3 #include <stdio.h>
4 #include < string.h>
5 #include <stdlib.h>
6
7 #define NUM_LEN 10
8 char num[][ 5] = {
9 { ' - ', ' - ', ' - ', ' - ', ' - '}, // 0
10 { ' . ', ' - ', ' - ', ' - ', ' - '}, // 1
11 { ' . ', ' . ', ' - ', ' - ', ' - '}, // 2
12 { ' . ', ' . ', ' . ', ' - ', ' - '}, // 3
13 { ' . ', ' . ', ' . ', ' . ', ' - '}, // 4
14 { ' . ', ' . ', ' . ', ' . ', ' . '}, // 5
15 { ' - ', ' . ', ' . ', ' . ', ' . '}, // 6
16 { ' - ', ' - ', ' . ', ' . ', ' . '}, // 7
17 { ' - ', ' - ', ' - ', ' . ', ' . '}, // 8
18 { ' - ', ' - ', ' - ', ' - ', ' . '} // 9
19 };
20
21
22 #define MARK_LEN 17
23 char mark[][ 8] = {
24 { ' . ', ' - ', ' . ', ' - ', ' . ', ' - ', ' * ', ' . '}, // . 0
25 { ' - ', ' - ', ' - ', ' . ', ' . ', ' . ', ' * ', ' : '}, // :
26 { ' - ', ' - ', ' . ', ' . ', ' - ', ' - ', ' * ', ' , '}, // ,
27 { ' - ', ' . ', ' - ', ' . ', ' - ', ' . ', ' * ', ' ; '}, // ;
28 { ' . ', ' . ', ' - ', ' - ', ' . ', ' . ', ' * ', ' ? '}, // ?
29 { ' - ', ' . ', ' . ', ' . ', ' - ', ' * ', ' * ', ' = '}, // =
30 { ' . ', ' - ', ' - ', ' - ', ' - ', ' . ', ' * ', ' \' '}, // '
31 { ' - ', ' . ', ' . ', ' - ', ' . ', ' * ', ' * ', ' / '}, ///
32 { ' - ', ' . ', ' - ', ' . ', ' - ', ' - ', ' * ', ' ! '}, // !
33 { ' - ', ' . ', ' . ', ' . ', ' . ', ' - ', ' * ', ' - '}, // -
34 { ' . ', ' . ', ' - ', ' - ', ' . ', ' - ', ' * ', ' _ '}, // _
35 { ' . ', ' - ', ' . ', ' . ', ' - ', ' . ', ' * ', ' " '}, // "
36 { ' - ', ' . ', ' - ', ' - ', ' . ', ' * ', ' * ', ' ( '}, // (
37 { ' - ', ' . ', ' - ', ' - ', ' . ', ' - ', ' * ', ' ) '}, // )
38 { ' . ', ' . ', ' . ', ' - ', ' . ', ' . ', ' - ', ' $ '}, // $
39 { ' . ', ' - ', ' . ', ' . ', ' . ', ' * ', ' * ', ' & '}, // &
40 { ' . ', ' - ', ' - ', ' . ', ' - ', ' . ', ' * ', ' @ '} // @ 16
41 };
42
43 #define CHARACTER 26
44 char a2[][ 4] = {
45 { ' . ', ' - ', ' * ', ' * '}, // A
46 { ' - ', ' . ', ' . ', ' . '}, // B
47 { ' - ', ' . ', ' - ', ' . '}, // C
48 { ' - ', ' . ', ' . ', ' * '}, // D
49 { ' . ', ' * ', ' * ', ' * '}, // E
50 { ' . ', ' . ', ' - ', ' . '}, // F
51 { ' - ', ' - ', ' . ', ' * '}, // G
52 { ' . ', ' . ', ' . ', ' . '}, // H
53 { ' . ', ' . ', ' * ', ' * '}, // I
54 { ' . ', ' - ', ' - ', ' - '}, // J
55 { ' - ', ' . ', ' - ', ' * '}, // K
56 { ' . ', ' - ', ' . ', ' . '}, // L
57 { ' - ', ' - ', ' * ', ' * '}, // M
58 { ' - ', ' . ', ' * ', ' * '}, // N
59 { ' - ', ' - ', ' - ', ' * '}, // O
60 { ' . ', ' - ', ' - ', ' . '}, // P
61 { ' - ', ' - ', ' . ', ' - '}, // Q
62 { ' . ', ' - ', ' . ', ' * '}, // R
63 { ' . ', ' . ', ' . ', ' * '}, // S
64 { ' - ', ' * ', ' * ', ' * '}, // T
65 { ' . ', ' . ', ' - ', ' * '}, // U
66 { ' . ', ' . ', ' . ', ' - '}, // V
67 { ' . ', ' - ', ' - ', ' * '}, // W
68 { ' - ', ' . ', ' . ', ' - '}, // X
69 { ' - ', ' . ', ' - ', ' - '}, // Y
70 { ' - ', ' - ', ' . ', ' . '} // Z
71 };
72
73 Morse_t *new_morse() {
74 Morse_t *ret;
75 ret = (Morse_t*) malloc( sizeof(Morse_t));
76 memset(ret->c, 0, 9);
77 return ret;
78 }
79
80
81 /*
82 * MARK
83 */
84 bool mark2morse( char n, Morse_t *morse) {
85 int a = 0;
86
87 for (; a < MARK_LEN; a++) {
88 if (mark[a][ 7] == n) {
89 morse->c[ 0] = mark[a][ 0];
90 morse->c[ 1] = mark[a][ 1];
91 morse->c[ 2] = mark[a][ 2];
92 morse->c[ 3] = mark[a][ 3];
93 morse->c[ 4] = mark[a][ 4];
94 morse->c[ 5] = mark[a][ 5];
95 morse->c[ 6] = mark[a][ 6];
96 return true;
97 }
98 }
99 return false;
100 }
101
102 bool morse2mark(Morse_t *morse, char *n) {
103 int a = 0;
104 for (; a < MARK_LEN; a++) {
105 if (mark[a][ 0] == morse->c[ 0] &&
106 mark[a][ 1] == morse->c[ 1] &&
107 mark[a][ 2] == morse->c[ 2] &&
108 mark[a][ 3] == morse->c[ 3] &&
109 mark[a][ 4] == morse->c[ 4] &&
110 mark[a][ 5] == morse->c[ 5] &&
111 mark[a][ 6] == morse->c[ 6] ) {
112
113 *n = mark[a][ 7];
114 return true;
115 }
116 }
117 return false;
118 }
119
120 /*
121 * NUMBER
122 */
123 bool num2morse( char n, Morse_t *morse) {
124 int pos = n - 48;
125
126 if (pos <= 9 && pos >= 0) {
127 morse->c[ 0] = num[pos][ 0];
128 morse->c[ 1] = num[pos][ 1];
129 morse->c[ 2] = num[pos][ 2];
130 morse->c[ 3] = num[pos][ 3];
131 morse->c[ 4] = num[pos][ 4];
132 return true;
133 }
134 return false;
135 }
136
137 bool morse2num(Morse_t *morse, char *n) {
138 int i = 0;
139
140 for (; i < NUM_LEN; i++) {
141 if (num[i][ 0] == morse->c[ 0] &&
142 num[i][ 1] == morse->c[ 1] &&
143 num[i][ 2] == morse->c[ 2] &&
144 num[i][ 3] == morse->c[ 3] &&
145 num[i][ 4] == morse->c[ 4] ) {
146
147 *n = ( char)(i + 48);
148 return true;
149 }
150 }
151 return false;
152 }
153
154 /*
155 * CHARACTER
156 */
157 bool str2morse( char m , Morse_t *morse) {
158 int pos = m - 97;
159 if (pos >= 0 && pos <= 25) {
160 morse->c[ 0] = a2[pos][ 0];
161 morse->c[ 1] = a2[pos][ 1];
162 morse->c[ 2] = a2[pos][ 2];
163 morse->c[ 3] = a2[pos][ 3];
164 return true;
165 }
166 return false;
167 }
168
169
170 bool morse2str(Morse_t *morse, char *ch) {
171 int i = 0;
172 for (i = 0; i < CHARACTER; i++) {
173 if (a2[i][ 0] == morse->c[ 0] &&
174 a2[i][ 1] == morse->c[ 1] &&
175 a2[i][ 2] == morse->c[ 2] &&
176 a2[i][ 3] == morse->c[ 3]) {
177
178 *ch = ( char)(i + 97);
179 return true;
180 }
181 }
182 return false;
183 }
184
185
186 void MorseString2String( char *morse , char * string, int buf_len) {
187 Morse_t *temp = new_morse();
188 int a = 0;
189 int b = 0;
190 int c = 0;
191 int len = 0;
192 char ch = ' * ';
193 memset(temp->c, ' * ', 8);
194 len = strlen(morse);
195
196 for ( ; a < len; a ++) {
197 if (c > buf_len) {
198 printf( " the string buffer is too little\n ");
199 return;
200 }
201
202 if (morse[a] != SEPARATOR && morse[a] != FAKE_SPACE)
203 temp->c[b++] = morse[a];
204 else if (morse[a] == SEPARATOR && morse[a- 1] != FAKE_SPACE) { // get one charactor
205 if ( true == morse2str(temp, &ch) && b < 5) {
206 string[c++] = ch;
207 } else if ( true == morse2num(temp, &ch)) {
208 string[c++] = ch;
209 } else if ( true == morse2mark(temp, &ch)) {
210 string[c++] = ch;
211 } else {
212 printf( " has morse that not be decoded !\n ");
213 }
214
215 // clean
216 b = 0;
217 memset(temp->c, ' * ' , 8);
218 } else if (morse[a] == FAKE_SPACE) { // have a space
219 string[c++] = ' ';
220 }
221 }
222 }
223
224 void String2MorseString( char * string , char *morse, int buf_len) {
225 int a = 0;
226 int b = 0;
227 int len = strlen( string);
228 Morse_t * temp = new_morse();
229
230 for (; a < len; a ++ ) {
231 if (buf_len < 8 || b >= buf_len) {
232 printf( " morse buffer is too litte\n ");
233 break;
234 }
235
236 if ( string[a] != ' ') {
237 // if is a num
238 if ( string[a] >= ' 0 ' && string[a] <= ' 9 ') {
239 if ( true == num2morse( string[a], temp)) {
240 morse[b++] = temp->c[ 0];
241 morse[b++] = temp->c[ 1];
242 morse[b++] = temp->c[ 2];
243 morse[b++] = temp->c[ 3];
244 morse[b++] = temp->c[ 4];
245 } else {
246 printf( " encode on mumber error \n ");
247 return ;
248 }
249 }
250 // if is a character
251 else if ( string[a] >= 97 && string[a] <= 122) {
252 if ( true == str2morse( string[a], temp)) {
253 morse[b++] = temp->c[ 0];
254 if (temp->c[ 1] != ' * ')
255 morse[b++] = temp->c[ 1];
256 if (temp->c[ 2] != ' * ')
257 morse[b++] = temp->c[ 2];
258 if (temp->c[ 3] != ' * ')
259 morse[b++] = temp->c[ 3];
260 } else {
261 printf( " encode on str error \n ");
262 return ;
263 }
264 }
265 // if is a mark
266 else if ( string[a] <= 127) {
267 if ( true == mark2morse( string[a], temp)) {
268 morse[b++] = temp->c[ 0];
269 morse[b++] = temp->c[ 1];
270 morse[b++] = temp->c[ 2];
271 morse[b++] = temp->c[ 3];
272 morse[b++] = temp->c[ 4];
273 if (temp->c[ 5] != ' * ')
274 morse[b++] = temp->c[ 5];
275 if (temp->c[ 6] != ' * ')
276 morse[b++] = temp->c[ 6];
277 } else {
278 printf( " encode on mark error \n ");
279 return ;
280 }
281 } else {
282 printf( " out of the morse character \n ");
283 return ;
284 }
285
286 // clean
287 memset(temp->c, 0 , 8);
288
289 morse[b++] = SEPARATOR;
290
291 } else if ( string[a] == ' ') { // have a space and add / to instead
292 morse[b++] = FAKE_SPACE;
293 morse[b++] = SEPARATOR;
294 }
295 }
296
297 }
298
299 void str2lowcase( char *str, char * out, int buf_len) {
300 int len = strlen(str);
301 int a = 0;
302
303 if (len >= buf_len) {
304 printf( " buf is to low\n ");
305 return;
306 }
307
308 for (;a < len; a++) {
309 if (str[a] >= ' A ' && str[a] <= ' Z ') {
310 out[a] = str[a] + 32;
311 } else {
312 out[a] = str[a];
313 }
314 }
315 }
2
3 #include <stdio.h>
4 #include < string.h>
5 #include <stdlib.h>
6
7 #define NUM_LEN 10
8 char num[][ 5] = {
9 { ' - ', ' - ', ' - ', ' - ', ' - '}, // 0
10 { ' . ', ' - ', ' - ', ' - ', ' - '}, // 1
11 { ' . ', ' . ', ' - ', ' - ', ' - '}, // 2
12 { ' . ', ' . ', ' . ', ' - ', ' - '}, // 3
13 { ' . ', ' . ', ' . ', ' . ', ' - '}, // 4
14 { ' . ', ' . ', ' . ', ' . ', ' . '}, // 5
15 { ' - ', ' . ', ' . ', ' . ', ' . '}, // 6
16 { ' - ', ' - ', ' . ', ' . ', ' . '}, // 7
17 { ' - ', ' - ', ' - ', ' . ', ' . '}, // 8
18 { ' - ', ' - ', ' - ', ' - ', ' . '} // 9
19 };
20
21
22 #define MARK_LEN 17
23 char mark[][ 8] = {
24 { ' . ', ' - ', ' . ', ' - ', ' . ', ' - ', ' * ', ' . '}, // . 0
25 { ' - ', ' - ', ' - ', ' . ', ' . ', ' . ', ' * ', ' : '}, // :
26 { ' - ', ' - ', ' . ', ' . ', ' - ', ' - ', ' * ', ' , '}, // ,
27 { ' - ', ' . ', ' - ', ' . ', ' - ', ' . ', ' * ', ' ; '}, // ;
28 { ' . ', ' . ', ' - ', ' - ', ' . ', ' . ', ' * ', ' ? '}, // ?
29 { ' - ', ' . ', ' . ', ' . ', ' - ', ' * ', ' * ', ' = '}, // =
30 { ' . ', ' - ', ' - ', ' - ', ' - ', ' . ', ' * ', ' \' '}, // '
31 { ' - ', ' . ', ' . ', ' - ', ' . ', ' * ', ' * ', ' / '}, ///
32 { ' - ', ' . ', ' - ', ' . ', ' - ', ' - ', ' * ', ' ! '}, // !
33 { ' - ', ' . ', ' . ', ' . ', ' . ', ' - ', ' * ', ' - '}, // -
34 { ' . ', ' . ', ' - ', ' - ', ' . ', ' - ', ' * ', ' _ '}, // _
35 { ' . ', ' - ', ' . ', ' . ', ' - ', ' . ', ' * ', ' " '}, // "
36 { ' - ', ' . ', ' - ', ' - ', ' . ', ' * ', ' * ', ' ( '}, // (
37 { ' - ', ' . ', ' - ', ' - ', ' . ', ' - ', ' * ', ' ) '}, // )
38 { ' . ', ' . ', ' . ', ' - ', ' . ', ' . ', ' - ', ' $ '}, // $
39 { ' . ', ' - ', ' . ', ' . ', ' . ', ' * ', ' * ', ' & '}, // &
40 { ' . ', ' - ', ' - ', ' . ', ' - ', ' . ', ' * ', ' @ '} // @ 16
41 };
42
43 #define CHARACTER 26
44 char a2[][ 4] = {
45 { ' . ', ' - ', ' * ', ' * '}, // A
46 { ' - ', ' . ', ' . ', ' . '}, // B
47 { ' - ', ' . ', ' - ', ' . '}, // C
48 { ' - ', ' . ', ' . ', ' * '}, // D
49 { ' . ', ' * ', ' * ', ' * '}, // E
50 { ' . ', ' . ', ' - ', ' . '}, // F
51 { ' - ', ' - ', ' . ', ' * '}, // G
52 { ' . ', ' . ', ' . ', ' . '}, // H
53 { ' . ', ' . ', ' * ', ' * '}, // I
54 { ' . ', ' - ', ' - ', ' - '}, // J
55 { ' - ', ' . ', ' - ', ' * '}, // K
56 { ' . ', ' - ', ' . ', ' . '}, // L
57 { ' - ', ' - ', ' * ', ' * '}, // M
58 { ' - ', ' . ', ' * ', ' * '}, // N
59 { ' - ', ' - ', ' - ', ' * '}, // O
60 { ' . ', ' - ', ' - ', ' . '}, // P
61 { ' - ', ' - ', ' . ', ' - '}, // Q
62 { ' . ', ' - ', ' . ', ' * '}, // R
63 { ' . ', ' . ', ' . ', ' * '}, // S
64 { ' - ', ' * ', ' * ', ' * '}, // T
65 { ' . ', ' . ', ' - ', ' * '}, // U
66 { ' . ', ' . ', ' . ', ' - '}, // V
67 { ' . ', ' - ', ' - ', ' * '}, // W
68 { ' - ', ' . ', ' . ', ' - '}, // X
69 { ' - ', ' . ', ' - ', ' - '}, // Y
70 { ' - ', ' - ', ' . ', ' . '} // Z
71 };
72
73 Morse_t *new_morse() {
74 Morse_t *ret;
75 ret = (Morse_t*) malloc( sizeof(Morse_t));
76 memset(ret->c, 0, 9);
77 return ret;
78 }
79
80
81 /*
82 * MARK
83 */
84 bool mark2morse( char n, Morse_t *morse) {
85 int a = 0;
86
87 for (; a < MARK_LEN; a++) {
88 if (mark[a][ 7] == n) {
89 morse->c[ 0] = mark[a][ 0];
90 morse->c[ 1] = mark[a][ 1];
91 morse->c[ 2] = mark[a][ 2];
92 morse->c[ 3] = mark[a][ 3];
93 morse->c[ 4] = mark[a][ 4];
94 morse->c[ 5] = mark[a][ 5];
95 morse->c[ 6] = mark[a][ 6];
96 return true;
97 }
98 }
99 return false;
100 }
101
102 bool morse2mark(Morse_t *morse, char *n) {
103 int a = 0;
104 for (; a < MARK_LEN; a++) {
105 if (mark[a][ 0] == morse->c[ 0] &&
106 mark[a][ 1] == morse->c[ 1] &&
107 mark[a][ 2] == morse->c[ 2] &&
108 mark[a][ 3] == morse->c[ 3] &&
109 mark[a][ 4] == morse->c[ 4] &&
110 mark[a][ 5] == morse->c[ 5] &&
111 mark[a][ 6] == morse->c[ 6] ) {
112
113 *n = mark[a][ 7];
114 return true;
115 }
116 }
117 return false;
118 }
119
120 /*
121 * NUMBER
122 */
123 bool num2morse( char n, Morse_t *morse) {
124 int pos = n - 48;
125
126 if (pos <= 9 && pos >= 0) {
127 morse->c[ 0] = num[pos][ 0];
128 morse->c[ 1] = num[pos][ 1];
129 morse->c[ 2] = num[pos][ 2];
130 morse->c[ 3] = num[pos][ 3];
131 morse->c[ 4] = num[pos][ 4];
132 return true;
133 }
134 return false;
135 }
136
137 bool morse2num(Morse_t *morse, char *n) {
138 int i = 0;
139
140 for (; i < NUM_LEN; i++) {
141 if (num[i][ 0] == morse->c[ 0] &&
142 num[i][ 1] == morse->c[ 1] &&
143 num[i][ 2] == morse->c[ 2] &&
144 num[i][ 3] == morse->c[ 3] &&
145 num[i][ 4] == morse->c[ 4] ) {
146
147 *n = ( char)(i + 48);
148 return true;
149 }
150 }
151 return false;
152 }
153
154 /*
155 * CHARACTER
156 */
157 bool str2morse( char m , Morse_t *morse) {
158 int pos = m - 97;
159 if (pos >= 0 && pos <= 25) {
160 morse->c[ 0] = a2[pos][ 0];
161 morse->c[ 1] = a2[pos][ 1];
162 morse->c[ 2] = a2[pos][ 2];
163 morse->c[ 3] = a2[pos][ 3];
164 return true;
165 }
166 return false;
167 }
168
169
170 bool morse2str(Morse_t *morse, char *ch) {
171 int i = 0;
172 for (i = 0; i < CHARACTER; i++) {
173 if (a2[i][ 0] == morse->c[ 0] &&
174 a2[i][ 1] == morse->c[ 1] &&
175 a2[i][ 2] == morse->c[ 2] &&
176 a2[i][ 3] == morse->c[ 3]) {
177
178 *ch = ( char)(i + 97);
179 return true;
180 }
181 }
182 return false;
183 }
184
185
186 void MorseString2String( char *morse , char * string, int buf_len) {
187 Morse_t *temp = new_morse();
188 int a = 0;
189 int b = 0;
190 int c = 0;
191 int len = 0;
192 char ch = ' * ';
193 memset(temp->c, ' * ', 8);
194 len = strlen(morse);
195
196 for ( ; a < len; a ++) {
197 if (c > buf_len) {
198 printf( " the string buffer is too little\n ");
199 return;
200 }
201
202 if (morse[a] != SEPARATOR && morse[a] != FAKE_SPACE)
203 temp->c[b++] = morse[a];
204 else if (morse[a] == SEPARATOR && morse[a- 1] != FAKE_SPACE) { // get one charactor
205 if ( true == morse2str(temp, &ch) && b < 5) {
206 string[c++] = ch;
207 } else if ( true == morse2num(temp, &ch)) {
208 string[c++] = ch;
209 } else if ( true == morse2mark(temp, &ch)) {
210 string[c++] = ch;
211 } else {
212 printf( " has morse that not be decoded !\n ");
213 }
214
215 // clean
216 b = 0;
217 memset(temp->c, ' * ' , 8);
218 } else if (morse[a] == FAKE_SPACE) { // have a space
219 string[c++] = ' ';
220 }
221 }
222 }
223
224 void String2MorseString( char * string , char *morse, int buf_len) {
225 int a = 0;
226 int b = 0;
227 int len = strlen( string);
228 Morse_t * temp = new_morse();
229
230 for (; a < len; a ++ ) {
231 if (buf_len < 8 || b >= buf_len) {
232 printf( " morse buffer is too litte\n ");
233 break;
234 }
235
236 if ( string[a] != ' ') {
237 // if is a num
238 if ( string[a] >= ' 0 ' && string[a] <= ' 9 ') {
239 if ( true == num2morse( string[a], temp)) {
240 morse[b++] = temp->c[ 0];
241 morse[b++] = temp->c[ 1];
242 morse[b++] = temp->c[ 2];
243 morse[b++] = temp->c[ 3];
244 morse[b++] = temp->c[ 4];
245 } else {
246 printf( " encode on mumber error \n ");
247 return ;
248 }
249 }
250 // if is a character
251 else if ( string[a] >= 97 && string[a] <= 122) {
252 if ( true == str2morse( string[a], temp)) {
253 morse[b++] = temp->c[ 0];
254 if (temp->c[ 1] != ' * ')
255 morse[b++] = temp->c[ 1];
256 if (temp->c[ 2] != ' * ')
257 morse[b++] = temp->c[ 2];
258 if (temp->c[ 3] != ' * ')
259 morse[b++] = temp->c[ 3];
260 } else {
261 printf( " encode on str error \n ");
262 return ;
263 }
264 }
265 // if is a mark
266 else if ( string[a] <= 127) {
267 if ( true == mark2morse( string[a], temp)) {
268 morse[b++] = temp->c[ 0];
269 morse[b++] = temp->c[ 1];
270 morse[b++] = temp->c[ 2];
271 morse[b++] = temp->c[ 3];
272 morse[b++] = temp->c[ 4];
273 if (temp->c[ 5] != ' * ')
274 morse[b++] = temp->c[ 5];
275 if (temp->c[ 6] != ' * ')
276 morse[b++] = temp->c[ 6];
277 } else {
278 printf( " encode on mark error \n ");
279 return ;
280 }
281 } else {
282 printf( " out of the morse character \n ");
283 return ;
284 }
285
286 // clean
287 memset(temp->c, 0 , 8);
288
289 morse[b++] = SEPARATOR;
290
291 } else if ( string[a] == ' ') { // have a space and add / to instead
292 morse[b++] = FAKE_SPACE;
293 morse[b++] = SEPARATOR;
294 }
295 }
296
297 }
298
299 void str2lowcase( char *str, char * out, int buf_len) {
300 int len = strlen(str);
301 int a = 0;
302
303 if (len >= buf_len) {
304 printf( " buf is to low\n ");
305 return;
306 }
307
308 for (;a < len; a++) {
309 if (str[a] >= ' A ' && str[a] <= ' Z ') {
310 out[a] = str[a] + 32;
311 } else {
312 out[a] = str[a];
313 }
314 }
315 }