使用批处理逐字符转换用户输入

发布于 2024-12-13 11:06:41 字数 524 浏览 2 评论 0原文

好吧,基本上我想要一个简单的批处理程序,以这样的方式将 X 字符更改为 Y 字符,

a-b
b-c
c-d
d-e
e-f
etc etc etc

我查找字符串和其他变量技巧,但它不起作用。这是我尝试过的,你可以在“codeb”上看到,我尝试了一种替代方法,

set /p code=[paste your code here]
set code=%codea:~1%
set /a %code:~2% %codeb%

这基本上是我尝试将所有输入字符拆分为单独变量的方法。

如果您的感觉....下面的无聊是翻译的确切转换,

a='
b=v
c=x
d=s
e=w
f=d
g=f
h=g
i=u
j=h
k=j
l=k
m=n
n=b
o=i
p=o
q=\
r=e
s=a
t=r
u=y
v=c
w=q
x=z
y=t
z=/

本质上我应该能够将这个“v'rxg”“粘贴”到批处理中并按回车键,然后它显示“批”

ok basicly i want a simple batch program to change X characters to Y characters in a way like this

a-b
b-c
c-d
d-e
e-f
etc etc etc

ive looks up strings and other variable tricks but it doesnt work. heres what i tried and you can see on "codeb" where i tried an alternate method

set /p code=[paste your code here]
set code=%codea:~1%
set /a %code:~2% %codeb%

this was basically my way of attempting to split all the input characters into seperate variables.

if your feeling....bored below is the exact conversion for the translation

a='
b=v
c=x
d=s
e=w
f=d
g=f
h=g
i=u
j=h
k=j
l=k
m=n
n=b
o=i
p=o
q=\
r=e
s=a
t=r
u=y
v=c
w=q
x=z
y=t
z=/

essentially i should be able to "paste" this "v'rxg" into the batch and hit enter and it then displays "batch"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

貪欢 2024-12-20 11:06:41

我修改了 Andriy 的代码以获得更快的版本,该版本使用批处理变量而不是 FIND 文本文件。如下:

@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in (codechart.txt) do set %%a
:loop
set /P encoded=[paste your code here]
if "%encoded%" == "" goto :eof
set decoded=
call :decode
echo Result:
echo\%decoded%
goto loop

:decode
if "%encoded%" == "" goto :eof
set ec=%encoded:~0,1%
set encoded=%encoded:~1%
set dc=?
if defined %ec% set dc=!%ec%!
set decoded=%decoded%%dc%
goto decode

但是,当对特殊字符(无效的变量名称)进行编码时,我的版本不起作用。

编辑 我的代码与 Andriy 的代码的唯一区别是:

  • setlocal EnableDelayedExpansion:除了 %var 之外,此命令还需要使用 !var! 扩展%,允许在同一命令中使用两种扩展。
  • for /f "delims=" %%a in (codechart.txt) do set %%a:此命令获取转换行并执行 set 命令每一个,即 set a=' set b=v 等。这样,转换结果将存储在 Batch 变量中,其中包含原始字符的名称和值新角色的。
  • if Defined %ec% set dc=!%ec%!:此行通过直接替换来转换 ec 变量中的字符。例如,如果 ec 为“b”: if Defined b set dc=!b!;因为b变量存在并且包含“v”,则执行set dc=v

I modified Andriy's code for a faster version that use Batch variables instead of FIND text file. Here it is:

@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in (codechart.txt) do set %%a
:loop
set /P encoded=[paste your code here]
if "%encoded%" == "" goto :eof
set decoded=
call :decode
echo Result:
echo\%decoded%
goto loop

:decode
if "%encoded%" == "" goto :eof
set ec=%encoded:~0,1%
set encoded=%encoded:~1%
set dc=?
if defined %ec% set dc=!%ec%!
set decoded=%decoded%%dc%
goto decode

However, my version does not work when special characters (non-valid variable names) are encoded.

EDIT The only differences of my code vs. Andriy's are these:

  • setlocal EnableDelayedExpansion: This command is required to use !var! expansion besides %var%, allowing to use both expansions in the same command.
  • for /f "delims=" %%a in (codechart.txt) do set %%a: This command takes the lines of your conversions and execute a set command with each one, that is, set a=' set b=v etc. This way, the conversions are stored in Batch variables with the name of the original character and the value of the new character.
  • if defined %ec% set dc=!%ec%!: This line convert the character in ec variable via a direct replacement. For instance, if ec is "b": if defined b set dc=!b!; because b variable exists and contains a "v", then execute set dc=v.
青春有你 2024-12-20 11:06:41

好吧,这是一个您可以使用的工作脚本。它以文本文件的形式使用您的图表(在我的设置中名为 codechart.txt):

@ECHO OFF
:loop
SET /P encoded=[paste your code here]
IF [%encoded%]==[] GOTO :EOF
SET decoded=
CALL :decode
ECHO Result:
ECHO(%decoded%
GOTO loop

:decode
IF [%encoded%]==[] GOTO :EOF
SET ec=%encoded:~0,1%
SET encoded=%encoded:~1%
SET dc=?
FOR /F "delims==" %%C IN ('FIND "=%ec%" ^<codechart.txt') DO SET dc=%%C
SET decoded=%decoded%%dc%
GOTO decode

如果您输入非空字符串,它将产生结果。按 Enter 而不实际输入任何内容将终止批处理。如果在图表中找不到某个字符,它将在输出中表示为 ?

Well, here's a working script that you can play with. It uses your chart in the form of a text file (named codechart.txt in my setup):

@ECHO OFF
:loop
SET /P encoded=[paste your code here]
IF [%encoded%]==[] GOTO :EOF
SET decoded=
CALL :decode
ECHO Result:
ECHO(%decoded%
GOTO loop

:decode
IF [%encoded%]==[] GOTO :EOF
SET ec=%encoded:~0,1%
SET encoded=%encoded:~1%
SET dc=?
FOR /F "delims==" %%C IN ('FIND "=%ec%" ^<codechart.txt') DO SET dc=%%C
SET decoded=%decoded%%dc%
GOTO decode

If you enter a non-empty string, it will produce a result. Hitting Enter without actually entering anything will terminate the batch. If a character is not found in the chart, it will be represented as a ? in the output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文