China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-24 14:50
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] Can a batch file do Morse code conversion? View 1,212 Replies 9
Original Poster Posted 2010-08-22 21:27 ·  中国 广东 深圳 电信
新手上路
Credits 11
Posts 10
Joined 2010-07-09 14:27
15-year member
UID 170292
Gender Male
Status Offline
Can a batch file do Morse code conversion? The user enters English letters, then they are converted into Morse code and saved into a text file. I thought of using set, but I don't know if that will work. Right now I still have no idea at all, so I'm asking everyone for help!

Conversion format:
For example: I LOVE YOU
Convert to: .. .-.. --- ...- . -.-- --- ..-


Also attaching the conversion codes for Morse code and English:

A .-
I ..
Q --.-
Y -.--
7 --…
B -…
J .---
R .-.
Z --..
8 ---..
C -.-.
K -.-
S …
1 .----
9 ----.
D -..
L .-..
T -
2 ..---
0 -----
E .
M --
U ..-
3 …--
. .-.-.-
F ..-.
N -.
V …-
4 ….-
? ..--..
G --.
O ---
W .--
5 …..
, --..--
H ….
P .--.
X -..-
6 -….
Floor 2 Posted 2010-08-22 23:39 ·  中国 湖北 黄石 电信
中级用户
★★
Credits 330
Posts 244
Joined 2006-04-14 14:07
20-year member
UID 53823
Gender Male
From 湖北
Status Offline
for + set can easily do the encoding, I just don't know how to decode it. I can't see any pattern in it.
Floor 3 Posted 2010-08-22 23:52 ·  中国 湖北 黄石 电信
中级用户
★★
Credits 330
Posts 244
Joined 2006-04-14 14:07
20-year member
UID 53823
Gender Male
From 湖北
Status Offline
I got dizzy, but I see it now. So it turns out to be a one-to-one correspondence.

Encoding:

@echo off&setlocal enabledelayedexpansion
for /f "tokens=1,2" %%I in (code.ini) do set #%%I=%%J
for /f "delims=" %%I in (TestFile.txt) do (
set line=%%I&set newline=
for /l %%x in (0,1,1000) do if "!line:~%%x,1!" neq "" call:sub !line:~%%x,1!
echo !newline!
)
:sub
if "%1" equ "" (set "newline=%newline% "&goto:eof)
set "newline=%newline%!#%1! "
goto:eof


fix: After encoding, separate each word with spaces.

[ Last edited by freeants001 on 2010-8-23 at 00:00 ]
Floor 4 Posted 2010-08-23 12:26 ·  中国 广东 深圳 电信
新手上路
Credits 11
Posts 10
Joined 2010-07-09 14:27
15-year member
UID 170292
Gender Male
Status Offline
What you posted won't work... What I want is conversion after user input, and your code also seems to have some problems, the window flashes and disappears. No way to make it work!
Floor 5 Posted 2010-08-23 12:27 ·  中国 广东 深圳 电信
新手上路
Credits 11
Posts 10
Joined 2010-07-09 14:27
15-year member
UID 170292
Gender Male
Status Offline
It really is a correspondence relationship, one letter corresponds to one character.
Floor 6 Posted 2010-08-23 14:09 ·  中国 湖北 黄石 电信
中级用户
★★
Credits 330
Posts 244
Joined 2006-04-14 14:07
20-year member
UID 53823
Gender Male
From 湖北
Status Offline
Originally posted by DOS2BAT at 2010-8-23 12:26:
What you posted won't work... What I want is conversion after user input, and your code also seems to have some problems, the window flashes and disappears. No way to make it work!


There is no problem with the code in post #3. If you want conversion after user input, you can try the following:


@echo off&setlocal enabledelayedexpansion
for /f "tokens=1,2" %%I in ('more +12 "%~f0"') do set #%%I=%%J
:begin
set newline=
set/p line=Please input a string:
for /l %%x in (0,1,1000) do if "!line:~%%x,1!" neq "" call:sub !line:~%%x,1!
echo.string: !line!&echo.enCode: !newline!&echo.!newline!>>encode$$$.txt
goto:begin
:sub
if "%1" equ "" (set "newline=%newline% "&goto:eof)
set "newline=%newline%!#%1! "
goto:eof
A .-
I ..
Q --.-
Y -.--
7 --…
B -…
J .---
R .-.
Z --..
8 ---..
C -.-.
K -.-
S …
1 .----
9 ----.
D -..
L .-..
T -
2 ..---
0 -----
E .
M --
U ..-
3 …--
. .-.-.-
F ..-.
N -.
V …-
4 ….-
? ..--..
G --.
O ---
W .--
5 …..
, --..--
H ….
P .--.
X -..-
6 -….



[ Last edited by freeants001 on 2010-8-23 at 14:27 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
doshsyy +1 2010-08-24 09:45
Floor 7 Posted 2010-08-23 20:31 ·  中国 广东 深圳 电信
新手上路
Credits 11
Posts 10
Joined 2010-07-09 14:27
15-year member
UID 170292
Gender Male
Status Offline
Your conversion isn't standardized~ after conversion, the ciphertext can't be turned back into the original text! Maybe it's the spaces or some other nonstandard part, hope it can be fixed.
This is an online conversion website. If its conversion can be used, that would also work, but then people would need internet access to use it. Still don't want that.
http://blog.gxceo.com/gj/morse.html
Floor 8 Posted 2010-08-23 20:46 ·  中国 四川 电信
初级用户
★★
Credits 198
Posts 89
Joined 2007-09-06 23:22
18-year member
UID 96746
Gender Male
From 重庆
Status Offline
Batch Home already had it long ago: http://bbs.bathome.net/thread-6467-1-2.html
Floor 9 Posted 2010-08-24 09:48 ·  中国 浙江 杭州 电信
新手上路
Credits 19
Posts 15
Joined 2010-08-20 13:09
15-year member
UID 172706
Gender Male
Status Offline
The code in post #6 is completely correct, it's the replacement table you gave that's wrong! You wrote "-" as "—", and "..." as "…", those are not the same! So I changed it a bit:
@echo off&setlocal enabledelayedexpansion
for /f "tokens=1,2" %%I in ('more +12 "%~f0"') do set #%%I=%%J
:begin
set newline=
set/p line=Please input a string:
for /l %%x in (0,1,1000) do if "!line:~%%x,1!" neq "" call:sub !line:~%%x,1!
echo.string: !line!&echo.enCode: !newline!&echo.!newline!>>encode$$$.txt
goto:begin
:sub
if "%1" equ "" (set "newline=%newline% "&goto:eof)
set "newline=%newline%!#%1! "
goto:eof
A .-
I ..
Q --.-
Y -.--
7 --...
B -...
J .---
R .-.
Z --..
8 ---..
C -.-.
K -.-
S ...
1 .----
9 ----.
D -..
L .-..
T -
2 ..---
0 ----
E .
M --
U ..-
3 ...--
. .-.-.-
F ..-.
N -.
V ...-
4 ....-
? ..--..
G --.
O ---
W .--
5 .....
, --..--
H ....
P .--.
X -..-
6 -....
Floor 10 Posted 2010-08-27 21:35 ·  中国 广东 深圳 电信
新手上路
Credits 11
Posts 10
Joined 2010-07-09 14:27
15-year member
UID 170292
Gender Male
Status Offline
OK, thanks everyone, the code is so complicated~ I can't understand it, I'm a newbie, thanks for taking care of me!
Right now I'm studying how to translate the ciphertext, hehe. That way conversion can be done without going online too.
Forum Jump: