Last active
March 10, 2022 09:36
-
-
Save jacquerie/8493041a5c5848ddccc8 to your computer and use it in GitHub Desktop.
Small C library to convert from chars to Morse Code and back.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ctype.h> | |
#include <stdlib.h> | |
#include "morse.h" | |
/* | |
* Function morse_to_index by cypherpunks on Reddit. | |
* See: http://goo.gl/amr6A3 | |
*/ | |
int morse_to_index (const char* str) | |
{ | |
unsigned char sum = 0, bit; | |
for (bit = 1; bit; bit <<= 1) { | |
switch (*str++) { | |
case 0: | |
return sum | bit; | |
default: | |
return 0; | |
case '-': | |
sum |= bit; | |
/* FALLTHROUGH */ | |
case '.': | |
break; | |
} | |
} | |
return 0; | |
} | |
const char* char_to_morse (char c) | |
{ | |
if (islower(c)) | |
c += ('A' - 'a'); | |
return CHAR_TO_MORSE[(int) c]; | |
} | |
const char* morse_to_char (const char* str) | |
{ | |
return MORSE_TO_CHAR[morse_to_index(str)]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _morse_h | |
#define _morse_h | |
static const char* CHAR_TO_MORSE[128] = { | |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
NULL, "-.-.--", ".-..-.", NULL, NULL, NULL, NULL, ".----.", | |
"-.--.", "-.--.-", NULL, NULL, "--..--", "-....-", ".-.-.-", "-..-.", | |
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", | |
"---..", "----.", "---...", NULL, NULL, "-...-", NULL, "..--..", | |
".--.-.", ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", | |
"....", "..", ".---", "-.-", ".-..", "--", "-.", "---", | |
".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", | |
"-..-", "-.--", "--..", NULL, NULL, NULL, NULL, "..--.-", | |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
}; | |
static const char* MORSE_TO_CHAR[128] = { | |
NULL, NULL, "E", "T", "I", "N", "A", "M", | |
"S", "D", "R", "G", "U", "K", "W", "O", | |
"H", "B", "L", "Z", "F", "C", "P", NULL, | |
"V", "X", NULL, "Q", NULL, "Y", "J", NULL, | |
"5", "6", NULL, "7", NULL, NULL, NULL, "8", | |
NULL, "/", NULL, NULL, NULL, "(", NULL, "9", | |
"4", "=", NULL, NULL, NULL, NULL, NULL, NULL, | |
"3", NULL, NULL, NULL, "2", NULL, "1", "0", | |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, ":", | |
NULL, NULL, NULL, NULL, "?", NULL, NULL, NULL, | |
NULL, NULL, "\"", NULL, NULL, NULL, "@", NULL, | |
NULL, NULL, NULL, NULL, NULL, NULL, "'", NULL, | |
NULL, "-", NULL, NULL, NULL, NULL, NULL, NULL, | |
NULL, NULL, ".", NULL, "_", ")", NULL, NULL, | |
NULL, NULL, NULL, ",", NULL, "!", NULL, NULL, | |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL | |
}; | |
const char* char_to_morse(char); | |
const char* morse_to_char(const char*); | |
int morse_to_index (const char*); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment