hexadecimal to decimal batch file

前端 未结 3 745
情话喂你
情话喂你 2021-01-14 22:07

okay so I have been working an a batch file that converts a base ten decimal value in to hexadecimal but I can\'t get the for loops and the if statements down right every we

相关标签:
3条回答
  • 2021-01-14 22:29

    Save this with .bat extension:

    @if (@x)==(@y) @end /***** jscript comment ******
         @echo off
    
         cscript //E:JScript //nologo "%~f0" %*
         exit /b 0
    
     @if (@x)==(@y) @end ******  end comment *********/
    
     var args=WScript.Arguments;
     var number=args.Item(0);
    
     WScript.Echo(parseInt(number,16));
    

    And use it like

    for /f %%# in ('hexToDec.bat 10E4') do set "decimalNumber=%%#"
    echo %decimalNumber%
    
    0 讨论(0)
  • 2021-01-14 22:44

    Supposing you want to convert hexadecimal to decimal numbers:

    Why not simply using set /A to do the job on its own? set /A is capable of converting hexadecimal to decimal numbers when prefixed with 0x. See set /? for details.
    The line below stores the decimal number in DEC, supposing HEX contains a hexadecimal number:

    set /A DEC=0x%HEX%
    

    The following script prompts the user for a hex. number and displays its decimal representation:

    @echo off
    :LOOP
    set "HEX="
    set /P "HEX=Enter a hexadecimal number (up to 8 digits): "
    if not defined HEX goto :EOF
    set /A DEC=0x%HEX%
    echo The decimal representation of %HEX% is %DEC%.
    goto :LOOP
    

    Note that the hexadecimal number is limited to 8 digits (32 bits), and it is interpreted as signed value.


    Supposing you want to convert decimal to hexadecimal numbers:

    There is an undocumented built-in environment variable called =ExitCode that holds the hexadecimal character code (ASCII) of the current return or exit code (usually the ErrorLevel).
    The lines below store the hexadecimal number in HEX, supposing DEC contains a decimal number:

    cmd /C exit %DEC%
    set "HEX=%=ExitCode%"
    

    The following script prompts the user for a decimal number and displays its hex. representation:

    @echo off
    :LOOP
    set "DEC="
    set /P "DEC=Enter a decimal number (signed 32-bit integer): "
    if not defined DEC goto :EOF
    cmd /C exit %DEC%
    set "HEX=%=ExitCode%"
    for /F "tokens=* delims=0" %%Z in ("%HEX%") do set "HEX=%%Z"
    if not defined HEX set "HEX=0"
    echo The hexadecimal representation of %DEC% is %HEX%.
    goto :LOOP
    
    0 讨论(0)
  • 2021-01-14 22:44

    Phew - what a lot of Code. This is a little bit shorter:

    @echo off
    setlocal enabledelayedexpansion
    set "hex=0123456789ABCDEF"
    set /p INPUT=Enter a number (0-255):
    set /a high=%INPUT% / 16
    set /a low=%INPUT% %% 16
    echo in hexadecimal %INPUT% = !hex:~%high%,1! !hex:~%low%,1!
    
    0 讨论(0)
提交回复
热议问题