找到这篇文章说明你对摩尔斯电码很赶兴趣,而且你已掌握了摩尔斯电码的基础知识了。想更贴近的感受一下摩尔斯电码的魅力。或你有一个非常棒的关于莫尔斯电码想法而非常激动。如果是这样的话,那你或许会从我的代码中得到帮助,缩短你实现想法征途。如果真的能帮助你的话,那么我也会很高兴。我也是如此的喜欢摩尔斯电码。

有任何疑问或者有想法想与人一起分享,邮箱我:robert.cysy@gmail.com

我把代码放到了github上了 地址 https://github.com/robert1207/morse_encode.git

可以在Linux下用Makefile 编译,也可以在windows下新建工程添加github上的代码就可以了

下面是代码片段:

#include

#include

#include

#include "morse.h"

#define BUF_LEN 300

int main() {

char *mystr = "abcdefghijklmnopqrstuvwxyz0123456789.:,;?='/!-_\"()$&@";

char mor[BUF_LEN];

char str[BUF_LEN];

char out[BUF_LEN];

memset(out, 0, BUF_LEN);

memset(mor, 0, BUF_LEN);

memset(str, 0, BUF_LEN);

printf("base string:\n%s\n", mystr);

//TO LOWCASE

str2lowcase(mystr, out, BUF_LEN);

//TO MORSE STRING

String2MorseString(out , mor, BUF_LEN);

printf("\nget morse code string:\n%s\n" , mor);

//TO NORMAL STRING

MorseString2String(mor, str, BUF_LEN);

printf("\nget decode string:\n%s\n", str);

return 0;

}

#include "morse.h"

#include

#include

#include

#define NUM_LEN 10

char num[][5] = {

{'-','-','-','-','-'},//0

{'.','-','-','-','-'},//1

{'.','.','-','-','-'},//2

{'.','.','.','-','-'},//3

{'.','.','.','.','-'},//4

{'.','.','.','.','.'},//5

{'-','.','.','.','.'},//6

{'-','-','.','.','.'},//7

{'-','-','-','.','.'},//8

{'-','-','-','-','.'} //9

};

#define MARK_LEN 17

char mark[][8] = {

{'.', '-', '.', '-', '.', '-', '*', '.'},//.0

{'-', '-', '-', '.', '.', '.', '*', ':'},//:

{'-', '-', '.', '.', '-', '-', '*', ','},//,

{'-', '.', '-', '.', '-', '.', '*', ';'},//;

{'.', '.', '-', '-', '.', '.', '*', '?'},//?

{'-', '.', '.', '.', '-', '*', '*', '='},//=

{'.', '-', '-', '-', '-', '.', '*', '\''},//'

{'-', '.', '.', '-', '.', '*', '*', '/'},///

{'-', '.', '-', '.', '-', '-', '*', '!'},//!

{'-', '.', '.', '.', '.', '-', '*', '-'},//-

{'.', '.', '-', '-', '.', '-', '*', '_'},//_

{'.', '-', '.', '.', '-', '.', '*', '"'},//"

{'-', '.', '-', '-', '.', '*', '*', '('},//(

{'-', '.', '-', '-', '.', '-', '*', ')'},//)

{'.', '.', '.', '-', '.', '.', '-', '$'},//$

{'.', '-', '.', '.', '.', '*', '*', '&'},//&

{'.', '-', '-', '.', '-', '.', '*', '@'} //@16

};

#define CHARACTER 26

char a2[][4] = {

{'.','-','*','*'},//A

{'-','.','.','.'},//B

{'-','.','-','.'},//C

{'-','.','.','*'},//D

{'.','*','*','*'},//E

{'.','.','-','.'},//F

{'-','-','.','*'},//G

{'.','.','.','.'},//H

{'.','.','*','*'},//I

{'.','-','-','-'},//J

{'-','.','-','*'},//K

{'.','-','.','.'},//L

{'-','-','*','*'},//M

{'-','.','*','*'},//N

{'-','-','-','*'},//O

{'.','-','-','.'},//P

{'-','-','.','-'},//Q

{'.','-','.','*'},//R

{'.','.','.','*'},//S

{'-','*','*','*'},//T

{'.','.','-','*'},//U

{'.','.','.','-'},//V

{'.','-','-','*'},//W

{'-','.','.','-'},//X

{'-','.','-','-'},//Y

{'-','-','.','.'} //Z

};

Morse_t *new_morse() {

Morse_t *ret;

ret = (Morse_t*)malloc(sizeof(Morse_t));

memset(ret->c, 0, 9);

return ret;

}

/*

*MARK

*/

bool mark2morse(char n, Morse_t *morse) {

int a = 0;

for (; a < MARK_LEN; a++) {

if (mark[a][7] == n) {

morse->c[0] = mark[a][0];

morse->c[1] = mark[a][1];

morse->c[2] = mark[a][2];

morse->c[3] = mark[a][3];

morse->c[4] = mark[a][4];

morse->c[5] = mark[a][5];

morse->c[6] = mark[a][6];

return true;

}

}

return false;

}

bool morse2mark(Morse_t *morse, char *n) {

int a = 0;

for (; a < MARK_LEN; a++) {

if (mark[a][0] == morse->c[0] &&

mark[a][1] == morse->c[1] &&

mark[a][2] == morse->c[2] &&

mark[a][3] == morse->c[3] &&

mark[a][4] == morse->c[4] &&

mark[a][5] == morse->c[5] &&

mark[a][6] == morse->c[6] ) {

*n = mark[a][7];

return true;

}

}

return false;

}

/*

*NUMBER

*/

bool num2morse(char n, Morse_t *morse) {

int pos = n - 48;

if (pos <= 9 && pos >= 0) {

morse->c[0] = num[pos][0];

morse->c[1] = num[pos][1];

morse->c[2] = num[pos][2];

morse->c[3] = num[pos][3];

morse->c[4] = num[pos][4];

return true;

}

return false;

}

bool morse2num(Morse_t *morse, char *n) {

int i = 0;

for (; i < NUM_LEN; i++) {

if (num[i][0] == morse->c[0] &&

num[i][1] == morse->c[1] &&

num[i][2] == morse->c[2] &&

num[i][3] == morse->c[3] &&

num[i][4] == morse->c[4] ) {

*n = (char)(i + 48);

return true;

}

}

return false;

}

/*

*CHARACTER

*/

bool str2morse(char m , Morse_t *morse) {

int pos = m - 97;

if (pos >= 0 && pos <= 25) {

morse->c[0] = a2[pos][0];

morse->c[1] = a2[pos][1];

morse->c[2] = a2[pos][2];

morse->c[3] = a2[pos][3];

return true;

}

return false;

}

bool morse2str(Morse_t *morse, char *ch) {

int i = 0;

for (i = 0; i < CHARACTER; i++) {

if (a2[i][0] == morse->c[0] &&

a2[i][1] == morse->c[1] &&

a2[i][2] == morse->c[2] &&

a2[i][3] == morse->c[3]) {

*ch = (char)(i + 97);

return true;

}

}

return false;

}

void MorseString2String(char *morse ,char *string, int buf_len) {

Morse_t *temp = new_morse();

int a = 0;

int b = 0;

int c = 0;

int len = 0;

char ch = '*';

memset(temp->c, '*', 8);

len = strlen(morse);

for ( ; a < len; a ++) {

if (c > buf_len) {

printf("the string buffer is too little\n");

return;

}

if (morse[a] != SEPARATOR && morse[a] != FAKE_SPACE)

temp->c[b++] = morse[a];

else if (morse[a] == SEPARATOR && morse[a-1] != FAKE_SPACE) {//get one charactor

if (true == morse2str(temp, &ch) && b < 5) {

string[c++] = ch;

} else if (true == morse2num(temp, &ch)) {

string[c++] = ch;

} else if (true == morse2mark(temp, &ch)) {

string[c++] = ch;

} else {

printf("has morse that not be decoded !\n");

}

//clean

b = 0;

memset(temp->c, '*' ,8);

} else if (morse[a] == FAKE_SPACE) { //have a space

string[c++] = ' ';

}

}

}

void String2MorseString(char *string ,char *morse, int buf_len) {

int a = 0;

int b = 0;

int len = strlen(string);

Morse_t * temp = new_morse();

for (; a < len; a ++ ) {

if (buf_len < 8 || b >= buf_len) {

printf("morse buffer is too litte\n");

break;

}

if (string[a] != ' ') {

//if is a num

if (string[a] >= '0' && string[a] <= '9') {

if (true == num2morse(string[a], temp)) {

morse[b++] = temp->c[0];

morse[b++] = temp->c[1];

morse[b++] = temp->c[2];

morse[b++] = temp->c[3];

morse[b++] = temp->c[4];

} else {

printf("encode on mumber error \n");

return ;

}

}

//if is a character

else if (string[a] >= 97 && string[a] <= 122) {

if (true == str2morse(string[a], temp)) {

morse[b++] = temp->c[0];

if (temp->c[1] != '*')

morse[b++] = temp->c[1];

if (temp->c[2] != '*')

morse[b++] = temp->c[2];

if (temp->c[3] != '*')

morse[b++] = temp->c[3];

} else {

printf("encode on str error \n");

return ;

}

}

//if is a mark

else if (string[a] <= 127) {

if (true == mark2morse(string[a], temp)) {

morse[b++] = temp->c[0];

morse[b++] = temp->c[1];

morse[b++] = temp->c[2];

morse[b++] = temp->c[3];

morse[b++] = temp->c[4];

if (temp->c[5] != '*')

morse[b++] = temp->c[5];

if (temp->c[6] != '*')

morse[b++] = temp->c[6];

} else {

printf("encode on mark error \n");

return ;

}

} else {

printf("out of the morse character \n");

return ;

}

//clean

memset(temp->c, 0 , 8);

morse[b++] = SEPARATOR;

} else if (string[a] == ' ') { //have a space and add / to instead

morse[b++] = FAKE_SPACE;

morse[b++] = SEPARATOR;

}

}

}

void str2lowcase(char *str, char *out, int buf_len) {

int len = strlen(str);

int a = 0;

if (len >= buf_len) {

printf("buf is to low\n");

return;

}

for (;a < len; a++) {

if (str[a] >= 'A' && str[a] <= 'Z') {

out[a] = str[a] + 32;

} else {

out[a] = str[a];

}

}

}

#ifndef MORSE_H_

#define MORSE_H_

typedef int bool;

#define false 0

#define true 1

/*

* FAKE_SPACE IS MARING FOR A SPACE

*/

#define FAKE_SPACE '/'

/*

* THE CHARACTER THAT BETWEEN TWO MORSE STRING

*/

#define SEPARATOR ' '

typedef struct Morse Morse_t;

struct Morse{

char c[9];

};

Morse_t *new_morse();

bool str2morse(char m , Morse_t *morse);

bool morse2str(Morse_t *morse, char *ch);

bool mark2morse(char n, Morse_t *morse);

bool morse2mark(Morse_t *morse, char *n);

bool num2morse(char n, Morse_t *morse);

bool morse2num(Morse_t *morse, char *n);

void MorseString2String(char *morse ,char *string, int buf_len);

void String2MorseString(char *string ,char *morse, int buf_len);

void str2lowcase(char *str, char *out, int buf_len);

#endif /* MORSE_H_ */

c语言输入字母转换摩斯密码,摩尔斯电码(morse)转换英文字符串c语言代码相关推荐

  1. Escape/Unescape,HTML实体编码,敲击码(Tap code),摩尔斯电码(Morse Code)

    1.Escape/Unescape 加密解码/编码解码,又叫%u编码,采用UTF-16BE模式, Escape编码/加密,就是字符对应UTF-16 16进制表示方式前面加%u.Unescape解码/解 ...

  2. python字典表示摩尔斯电码_python转换字符串为摩尔斯电码的方法

    本文实例讲述了python转换字符串为摩尔斯电码的方法.分享给大家供大家参考.具体实现方法如下: chars = ",.0123456789?abcdefghijklmnopqrstuvwx ...

  3. 摩尔斯电码转换python编码_Morse Code 用Python做个摩斯密码转换器

    摩斯密码摩尔斯电码(又译为摩斯密码,Morse code)是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母.数字和标点符号,不同于现代只使用零和一两种状态的二进制代码,它的代码包括五种 ...

  4. arduino nano 蓝牙_基于Arduino的摩尔斯电码练习及无线收发报训练器

    摘要:本文介绍一款基于Arduino NANO开发的,带2.4G无线收发报功能的摩尔斯电码训练器的系统设计思路.发射端通过NANO板的外部中断引脚采集电键输入的脉冲PPM序列同时驱动喇叭播放电键音,然 ...

  5. 利用python实现将文本、摩尔斯电码与用滴答表示的摩尔斯电码三者相互转换

    文章目录 目录 前言 一.摩尔斯电码的对照表: 二.python代码的完整展示 三.代码运行结果的展示: 四.总结与注意事项 前言 本文的代码主要通过字典与反转字典,实现摩尔斯电码与文本的相互转换,并 ...

  6. 简单的摩尔斯电码(又译为摩斯电码)

    1.名字解析: 摩尔斯电码(Morse alphabet)(又译为摩斯电码)是一种时通时断的信号代码,这种信号代码通过不同的排列顺序来表达不同的英文字母.数字和标点符号等.由美国人摩尔斯(Samuel ...

  7. Mixly06:国际摩尔斯电码救难信号SOS

    S.O.S是国际摩尔斯电码救难信号. 船舶在浩瀚的大洋中航行,由于浓雾.风暴.冰山.暗礁.机器失灵.与其它船只相撞等等,往往会发生意外的事故.当死神向人们逼近时,"SOS"的遇难信 ...

  8. 摩尔斯电码(morse)转换英文字符串c语言代码

    找到这篇文章说明你对摩尔斯电码很赶兴趣,而且你已掌握了摩尔斯电码的基础知识了.想更贴近的感受一下摩尔斯电码的魅力.或你有一个非常棒的关于莫尔斯电码想法而非常激动.如果是这样的话,那你或许会从我的代码中 ...

  9. 摩尔斯电码转换python编码_python转换字符串为摩尔斯电码的方法

    python转换字符串为摩尔斯电码的方法 本文实例讲述了python转换字符串为摩尔斯电码的方法.分享给大家供大家参考.具体实现方法如下: chars = ",.0123456789?abc ...

最新文章

  1. Exception testing
  2. 深入理解Python生成器(Generator)
  3. Netty实战 IM即时通讯系统(六)实战: 客户端和服务端双向通信
  4. 腾讯Angel亮相VLDB,携全新Angel 2.0宣布加入LF深度学习基金会
  5. 通过反射创建新类示例的两种方式及比较
  6. (22)Xilinx FPGA PCIE中断接口(学无止境)
  7. gittrack_Git 分支跟踪详解(remote branch tracking)
  8. 【Python】使用Labelme标注自己的数据集并由json生成Ground Truth
  9. html设置表单禁止修改群名片,如何设置禁止管理员修改qq群名片
  10. Java基础知识总结(超详细整理)
  11. c语言 字母常量,C语言常量的类型
  12. 计算机慢怎么解决6,电脑运行速度慢怎么回事 电脑运行速度慢的解决方法
  13. 工控行业学什么编程语言比较好_机器人学中最流行的10种编程语言
  14. 移动端设计的基础尺寸单位与转化
  15. 通行宝通过注册:年营收5.9亿拟募资5.6亿 腾讯云与上汽是股东
  16. QT 5.15 最新安装指南(针对不同系统)
  17. LocalDate 计算两个日期相差天数
  18. leetcode算法(2)
  19. js 三大家族(offset/scroll/client)
  20. 【软件测试】敏捷方法与测试左移

热门文章

  1. mysql timespan_.NET对象序列化—TimeSpan
  2. 或普通或文艺或2B的Hello World
  3. ZipUtil压缩工具类坑点
  4. LeetCode 47 全排列 II -- 回溯法
  5. JavaScript分类显示随机颜色【红绿蓝青黄紫、黑白、全彩】
  6. Linux运维常问面试题总结
  7. 用C++实现数组的二分查找算法
  8. stripos使用查询某字母开头
  9. 神经网络与深度学习(三)——反向传播算法
  10. react循环渲染数据