0

I am trying to create a batch script which will convert letters to numbers. But I cannot get it to be case sensitive - any suggestion?

@ECHO OFF
setlocal
set init="A=1" "a=2" "B=3" "b=4" "C=5" "c=6"
set %init: =&set %
set "nickname= "
set /P "nickname=Enter here: "
set "nickname=%nickname: =%"
if not defined nickname goto :EOF
set "out="
set "nums="
set "sum=0"
:loop
   set "char=%nickname:~0,1%"
   set "out=%out%%char%"
   set /A "code=%char%, sum+=code"
   set "nums=%nums%%code%"
set "nickname=%nickname:~1%"
IF DEFINED nickname GOTO :loop
ECHO original: %out%
ECHO converted to: %nums%
pause

So if I enter Aa it converts to 22 - but should be 12. Thanks! :)

I have searched and tried different solutions to similar problems found on stackoverflow. But no luck so far. If I enter Aa it converts to 22 - but should be 12.

  • In a Command Prompt window, if you type ```echo %WINDIR%^|%windir%^|%wInDiR%```, and press the `[ENTER]` key, what do you see? and does that show you your inherent issue? – Compo Mar 28 '23 at 11:31

2 Answers2

0

There could be used the following code for this task:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

:UserPrompt
set "Nickname="
set /P "Nickname=Enter nickname: " || goto UserPrompt
rem Remove all double quotes from the entered string.
set "Nickname=%Nickname:"=%"
if not defined Nickname goto UserPrompt
rem Remove all spaces from the entered string.
set "Nickname=%Nickname: =%"
if not defined Nickname goto UserPrompt
set "Original=%Nickname%"
set "Numbers="

:Loop
for %%G in ("A=1" "a=2" "B=3" "b=4" "C=5" "c=6") do for /F "tokens=1,2 delims==" %%I in (%%G) do if "%Nickname:~0,1%" == "%%I" set "Numbers=%Numbers%%%J" & set "Nickname=%Nickname:~1%" & if defined Nickname (goto Loop) else goto OutputResult
set "Nickname=%Nickname:~1%"
if defined Nickname goto Loop

:OutputResult
setlocal EnableDelayedExpansion
echo Original: !Original!
echo Converted to: !Numbers!
endlocal
endlocal
pause

There is output for the user input "Asterix & Obelix":

Original: Asterix&Obelix
Converted to: 14

The code is fail-safe and secure which means independent on what the user inputs on prompt, the Windows Command Processor runs really the commands as written in the batch file and does never exit because of a syntax error caused by the string input by the user.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143
-1

Thanks to Mofi who posted the following comment which worked perfectly!

Replace the six lines below :loop by for %%G in ("A=1" "a=2" "B=3" "b=4" "C=5" "c=6") do for /F "tokens=1,2 delims==" %%I in (%%G) do if "%nickname:~0,1%" == "%%I" set "nums=%nums%%%J" & set "nickname=%nickname:~1%" & if defined nickname (goto loop) else goto OutputResult and replace ECHO original: %out% by :OutputResult.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • 3
    Please do not add comments directed to individuals to your answer. Please also, format the code you have posted. There is an `Edit` link for you to use, in order to achieve that. – Compo Mar 28 '23 at 13:28