Decimal to Hexadecimal & binary converter Batch program, Any suggestions how to implement it differently?

Viewed 4354

Ive been working on this decimal to hexadecimal and binary converter, the hexadecimal converts the decimal number into hex, and the binary converter does it's job.

This works just fine, but is there a different way to implement this code?

@echo off
setlocal EnableDelayedExpansion

:Main
setlocal
cls
set /p decIn=enter Decimal value: 
call :DectoHex %decIn% hexOut
call :DectoBin %decIn% binOut
echo !hexOut!
echo !binOut!
pause>nul
goto :main
endlocal

::-------------------------------------------------------------------------------------

:DectoHex
set /a A=%1
set map=0123456789ABCDEF
set H=

:Hexloop
set /a B=!A! %%16 & set /a A /=16 
set H=!map:~%B%,1!!H!
if !A! gtr 0 goto :Hexloop
set %2=!H!
goto :eof

::-------------------------------------------------------------------------------

:DectoBin
:: Note Dec Bit Bin (DBB)

set /a Dec=%1
set Bin=
for /L %%i in (1,1,32) do (
    set /a "Bit=Dec&1, Dec>>=1"
    set Bin=!Bit!!Bin!
)


:skimming
    if %Bin:~0,1% == 0 (
    set Bin=%Bin:~1% & goto :skimming

)

set %2=!Bin!
goto :eof

::-----------------------------------------------------------------------------

:BintoDec
set bin=11011
set dig=
set digval=1

set dec=

:Loop
if %bin% gtr 1 (
    set dig=%bin:~-1%
    set bin=%bin:~0,-1%
) else (
    set /a dig=%bin%
    set bin=0
)

if %dig% equ 1 (
    set /a dec+=%digval%
)
set digval *=2

if %bin% gtr 0 goto :loop

echo %dec%

pause>nul

goto :eof

The main function recieves an input from the user and passes the variable as a parameter to a hexadecimal converter and a binary converter, the

2 Answers

In a function, it gaves that.

(Put in in a UTF-8 .txt file with BOM EFBBBF, first line must be blank)

cls
@echo off
cd /d "%~dp0"
chcp 65001 >nul
set title_script=Base conversion
title %title_script%

set "decimal_number=200"
set "result_hexadecimal="
call :function_change_base_number 10 decimal_number 16 result_hexadecimal
echo %decimal_number% base 10 = %result_hexadecimal% base 16
echo.

set "octal_number=310"
set "result_binary="
call :function_change_base_number 8 octal_number 2 result_binary
echo %octal_number% base 8 = %result_binary% base 2
echo.

set "quaternary_number=3020"
set "base_36_result="
call :function_change_base_number 4 quaternary_number 36 base_36_result
echo %quaternary_number% base 4 = %base_36_result% base 36
echo.

pause
exit

:function_change_base_number
    setlocal enabledelayedexpansion
    set /a input_base=%~1
    set input_number=!%~2!
    set /a output_base=%~3

    rem UCase
    for %%i in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") do (
        call set "input_number=!input_number:%%~i!"
    )

    set map=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
    set map_base=!map:~0,%input_base%!
    set /a position=0
    set input_number_check=%input_number%
    :loop_1
        set current_map_base_character=!map_base:~%position%,1!
        set input_number_check=!input_number_check:%current_map_base_character%=!
        if "!input_number_check!" == "" goto next_1
        set /a position+=1
        if "!map_base:~%position%,1!" NEQ "" goto loop_1
        echo Error.
        echo Invalid character in base %input_base%.
        echo.
        pause
        exit
    :next_1

    set /a carry_flag=1
    set "input_number_tmp=%input_number%"
    set "result_decimal="
    :loop_2
        set "input_number_tmp_digit=%input_number_tmp:~-1%"
        if %input_number_tmp_digit% LEQ 9 goto next_2
        for /l %%i in (10,1,%input_base%) do (
            if /i "%input_number_tmp_digit%" EQU "!map:~%%i,1!" (
                set "input_number_tmp_digit=%%i"
                goto :next_2
            )
        )
        echo Error.
    :next_2
    set /a result_decimal+=input_number_tmp_digit * carry_flag
    set /a carry_flag *= input_base
    set "input_number_tmp=%input_number_tmp:~0,-1%"
    if defined input_number_tmp goto :loop_2

    set /a input_number_tmp=%result_decimal%
    set "result="
    :loop_3
        set /a "division_remainder=input_number_tmp %% !output_base!"
        set /a "input_number_tmp /= !output_base!"
        set result=!map:~%division_remainder%,1!%result%
        if "%input_number_tmp%" NEQ "0" goto loop_3
    endlocal & set %~4=%result%
goto :eof
Related