Script snippet presented below exemplifies the usage.
BinToHex
"procedure" now works locally. Validity of the input is to be verified before passing it to the function.
ERRORLEVEL
is set to 1 if input is empty or if it encounters illegal character.
Update: performance improved.
@echo off
:loop
echo.
set/p input= give binary number
call:BinToHex input output
if "%errorlevel%"=="1" (echo Conversion failed.) else (echo [%input%] == [0x%output%])
goto loop
:BinToHex
setlocal
set var1=%% %1 %%
set var1=%var1: =%
call set var1=%var1%
:bintohex_lp
call:bintohex_ [%var1:~-4%]||goto bintohex_failure
set var0=%var2%%var0%&set var1=%var1:~0,-4%
if not defined var1 (goto bintohex_end) else (goto bintohex_lp)
:bintohex_
if "%1"=="[0000]" set var2=0&exit/b0
if "%1"=="[0001]" set var2=1&exit/b0
if "%1"=="[0010]" set var2=2&exit/b0
if "%1"=="[0011]" set var2=3&exit/b0
if "%1"=="[0100]" set var2=4&exit/b0
if "%1"=="[0101]" set var2=5&exit/b0
if "%1"=="[0110]" set var2=6&exit/b0
if "%1"=="[0111]" set var2=7&exit/b0
if "%1"=="[1000]" set var2=8&exit/b0
if "%1"=="[1001]" set var2=9&exit/b0
if "%1"=="[1010]" set var2=A&exit/b0
if "%1"=="[1011]" set var2=B&exit/b0
if "%1"=="[1100]" set var2=C&exit/b0
if "%1"=="[1101]" set var2=D&exit/b0
if "%1"=="[1110]" set var2=E&exit/b0
if "%1"=="[1111]" set var2=F&exit/b0
if "%1"=="[111]" set var2=7&exit/b0
if "%1"=="[110]" set var2=6&exit/b0
if "%1"=="[101]" set var2=5&exit/b0
if "%1"=="[100]" set var2=4&exit/b0
if "%1"=="[011]" set var2=3&exit/b0
if "%1"=="[010]" set var2=2&exit/b0
if "%1"=="[001]" set var2=1&exit/b0
if "%1"=="[000]" set var2=0&exit/b0
if "%1"=="[11]" set var2=3&exit/b0
if "%1"=="[10]" set var2=2&exit/b0
if "%1"=="[01]" set var2=1&exit/b0
if "%1"=="[00]" set var2=0&exit/b0
if "%1"=="[1]" set var2=1&exit/b0
if "%1"=="[0]" set var2=0&exit/b0
exit/b1
:bintohex_failure
set var0=&set temp_err=1
:bintohex_end
endlocal&set %2=%var0%&exit/b%temp_err%
No comments:
Post a Comment