2016-03-07 65 views
2

從我所看到的變量'!text %% b!'沒有被設置到變量'數字'由於某種未知的原因。這個奇怪的錯誤阻止了我繼續執行代碼,我發現沒有理由不工作。變量未被插入到另一個變量中,原因不明:將文本轉換爲二進制

@echo on 
Setlocal EnableDelayedExpansion 
if "%1"=="/?" (
    call :help 
    exit /b 
) 
if "%1"=="/b" (
    set text=%2 %3 %4 %5 %6 %7 %8 %9 
    call :convBinary 
    exit /b 
) 
if "%1"=="/t" (
    set text=%2 %3 %4 %5 %6 %7 %8 %9 
    call :convText 
    exit /b 
) 
echo [CONV] That command is not recognized. 
exit /b 

:help 
echo Converts any text or binary to either text or binary. 
echo. 
echo Stynax: CONV [conversion] {options} [text] 
echo. 
echo /b  Converts given text to binary. 
echo /t  Converts given binary to text. 
echo. 
exit /b 

:convBinary 
for /L %%a in (0, 1, 100) do (
    set text%%a=!text:~%%a,1! 
) 
for /L %%b in (0, 1, 100) do (
    if "!text%%b!"=="A" (
     set text%%b=65 
    ) 
    if "!text%%b!"=="B" (
     set text%%b=66 
    ) 
    if "!text%%b!"=="C" (
     set text%%b=67 
    ) 
    if "!text%%b!"=="D" (
     set text%%b=68 
    ) 
    if "!text%%b!"=="E" (
     set text%%b=69 
    ) 
    if "!text%%b!"=="F" (
     set text%%b=70 
    ) 
    if "!text%%b!"=="G" (
     set text%%b=71 
    ) 
    if "!text%%b!"=="H" (
     set text%%b=72 
    ) 
    if "!text%%b!"=="I" (
     set text%%b=73 
    ) 
    if "!text%%b!"=="J" (
     set text%%b=74 
    ) 
    if "!text%%b!"=="K" (
     set text%%b=75 
    ) 
    if "!text%%b!"=="L" (
     set text%%b=76 
    ) 
    if "!text%%b!"=="M" (
     set text%%b=77 
    ) 
    if "!text%%b!"=="N" (
     set text%%b=78 
    ) 
    if "!text%%b!"=="O" (
     set text%%b=79 
    ) 
    if "!text%%b!"=="P" (
     set text%%b=80 
    ) 
    if "!text%%b!"=="Q" (
     set text%%b=81 
    ) 
    if "!text%%b!"=="R" (
     set text%%b=82 
    ) 
    if "!text%%b!"=="S" (
     set text%%b=83 
    ) 
    if "!text%%b!"=="T" (
     set text%%b=84 
    ) 
    if "!text%%b!"=="U" (
     set text%%b=85 
    ) 
    if "!text%%b!"=="V" (
     set text%%b=86 
    ) 
    if "!text%%b!"=="W" (
     set text%%b=87 
    ) 
    if "!text%%b!"=="X" (
     set text%%b=88 
    ) 
    if "!text%%b!"=="Y" (
     set text%%b=89 
    ) 
    if "!text%%b!"=="Z" (
     set text%%b=90 
    ) 
    if "!text%%b!"=="a" (
     set text%%b=97 
    ) 
    if "!text%%b!"=="b" (
     set text%%b=98 
    ) 
    if "!text%%b!"=="c" (
     set text%%b=99 
    ) 
    if "!text%%b!"=="d" (
     set text%%b=100 
    ) 
    if "!text%%b!"=="e" (
     set text%%b=101 
    ) 
    if "!text%%b!"=="f" (
     set text%%b=102 
    ) 
    if "!text%%b!"=="g" (
     set text%%b=103 
    ) 
    if "!text%%b!"=="h" (
     set text%%b=104 
    ) 
    if "!text%%b!"=="i" (
     set text%%b=105 
    ) 
    if "!text%%b!"=="j" (
     set text%%b=106 
    ) 
    if "!text%%b!"=="k" (
     set text%%b=107 
    ) 
    if "!text%%b!"=="l" (
     set text%%b=108 
    ) 
    if "!text%%b!"=="m" (
     set text%%b=109 
    ) 
    if "!text%%b!"=="n" (
     set text%%b=110 
    ) 
    if "!text%%b!"=="o" (
     set text%%b=111 
    ) 
    if "!text%%b!"=="p" (
     set text%%b=112 
    ) 
    if "!text%%b!"=="q" (
     set text%%b=113 
    ) 
    if "!text%%b!"=="r" (
     set text%%b=114 
    ) 
    if "!text%%b!"=="s" (
     set text%%b=115 
    ) 
    if "!text%%b!"=="t" (
     set text%%b=116 
    ) 
    if "!text%%b!"=="u" (
     set text%%b=117 
    ) 
    if "!text%%b!"=="v" (
     set text%%b=118 
    ) 
    if "!text%%b!"=="w" (
     set text%%b=119 
    ) 
    if "!text%%b!"=="x" (
     set text%%b=120 
    ) 
    if "!text%%b!"=="y" (
     set text%%b=121 
    ) 
    if "!text%%b!"=="z" (
     set text%%b=122 
    ) 
    if "!text%%b!"=="" (
     set text%%b= 
    ) 
    set ascii=!ascii! !text%%b! 
    set number=!text%%b! 
    if "!text%%b!"==" " (
     set nul=0 
    ) ELSE (
     set /a binary1=%number% %% 2 
     set /a number=%number%/2 
     set /a binary2=%number% %% 2 
     set /a number=%number%/2 
     set /a binary3=%number% %% 2 
     set /a number=%number%/2 
     set /a binary4=%number% %% 2 
     set /a number=%number%/2 
     set /a binary5=%number% %% 2 
     set /a number=%number%/2 
     set /a binary6=%number% %% 2 
     set /a number=%number%/2 
     set /a binary7=%number% %% 2 
     set /a number=%number%/2 
     set /a binary8=%number% %% 2 
     set /a number=%number%/2 
     set outBin=%outBin% %binary8%%binary7%%binary6%%binary5%%binary4%%binary3%%binary2%%binary1% 
    ) 
) 
echo INPUT: !text! 
echo ASCII: !ascii! 
echo BINARY:!outBin! 
exit /b 

回答

1

SomethingDark解決了您的問題,但我簡化了代碼。

@echo off 
Setlocal EnableDelayedExpansion 
if "%1"=="/?" (
    call :help 
    exit /b 
) 


rem set Upper case ascii values 
    for /L %%a in (65, 1, 90) do (
     cmd /c exit /b %%a 
     set AsciiU.!=ExitCodeAscii!.=.%%a 
    ) 

rem set Lower case ascii values 
    for /L %%a in (97, 1, 122) do (
     cmd /c exit /b %%a 
     set AsciiL.!=ExitCodeAscii!.=.%%a 
    ) 
    rem set asciiU & pause 
    rem set asciiL & pause 

rem separate the input text from the switch, and remove any spaces 
    set "text=%*" 
    set "text=%text:/b =%" 
    set "text=%text:/t =%" 
    set "input=%text%" 
    set "text=%text: =%" 

if "%1"=="/b" (
    call :convBinary 
    pause 
    exit /b 
) 
if "%1"=="/t" (
    call :convText 
    pause 
    exit /b 
) 
echo [CONV] That command is not recognized. 
exit /b 

:help 
echo Converts text or binary, or binary to text. 
echo. 
echo Stynax: CONV [conversion] {options} [text] 
echo. 
echo /b  Converts given text to binary. 
echo /t  Converts given binary to text. 
echo. 
exit /b 

:convBinary 
set "ResultAscii=" 
for /L %%a in (0, 1, 100) do (
    if "!text:~%%a,1!"=="" goto :set 
    for /f "tokens=2,4 delims=." %%c in ('set ascii') do (
    if "!text:~%%a,1!"=="%%c" set ResultAscii=!ResultAscii! %%d 
    ) 
) 
:set 
set "ResultBin=" 
for %%a in (!ResultAscii!) do (
    set number=%%a 
     set /a binary1=number %% 2, number=number/2 
     set /a binary2=number %% 2, number=number/2 
     set /a binary3=number %% 2, number=number/2 
     set /a binary4=number %% 2, number=number/2 
     set /a binary5=number %% 2, number=number/2 
     set /a binary6=number %% 2, number=number/2 
     set /a binary7=number %% 2, number=number/2 
     set /a binary8=number %% 2, number=number/2 
     set ResultBin=!ResultBin! !binary8!!binary7!!binary6!!binary5!!binary4!!binary3!!binary2!!binary1! 
) 

echo INPUT: !input! 
echo ASCII: !ResultAscii:~1! 
echo BINARY:!ResultBin:~1! 
goto :EOF 
+0

謝謝Foxidrive,你真的幫助簡化了代碼並解決了問題。順便說一下,我對使用可變擴展和延遲擴展很陌生,所以我不知道發生了什麼。 –

1

當您創建的第一次,這意味着它們不會在初始變量擴展過程中定義的%number%%outBin%%binary1-8%變量你還是一個for循環中。將它們替換爲延遲擴展版本。

set /a binary1=!number!%%2 
    set /a number=!number!/2 
    set /a binary2=!number!%%2 
    set /a number=!number!/2 
    set /a binary3=!number!%%2 
    set /a number=!number!/2 
    set /a binary4=!number!%%2 
    set /a number=!number!/2 
    set /a binary5=!number!%%2 
    set /a number=!number!/2 
    set /a binary6=!number!%%2 
    set /a number=!number!/2 
    set /a binary7=!number!%%2 
    set /a number=!number!/2 
    set /a binary8=!number!%%2 
    set /a number=!number!/2 
    set outBin=!outBin! !binary8!!binary7!!binary6!!binary5!!binary4!!binary3!!binary2!!binary1! 
+0

謝謝SomethingDark用於識別問題並提供解釋。 –