Powered by GeSHi

Source code for dec2hex.bat

(view source code of dec2hex.bat as plain text)

  1. @ECHO OFF
  2. :: Windows NT / 2000 / XP only
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: One command line parameter only
  6. IF     "%~1"=="" GOTO Syntax
  7. IF NOT "%~2"=="" GOTO Syntax
  8.  
  9. :: Display help if requested
  10. ECHO "%~1" | FIND "?" >NUL
  11. IF NOT ERRORLEVEL 1 GOTO Syntax
  12. ECHO "%~1" | FIND "/" >NUL
  13. IF NOT ERRORLEVEL 1 GOTO Syntax
  14.  
  15. :: Check if command line parameter is a valid number
  16. ECHO "%~1"| FINDSTR /R /B /C:"\"[0-9]*\"$" >NUL
  17. IF ERRORLEVEL 1 GOTO Syntax
  18.  
  19. :: Enable delayed variable expansion
  20. SETLOCAL ENABLEDELAYEDEXPANSION
  21.  
  22. :: Use PING to split the number into 4 2-digit hexadecimal numbers that
  23. :: are displayed again as 4 decimal numbers from 0 to 255 (FOD or Four
  24. :: Octet Decimal)
  25. FOR /F "tokens=2 delims= " %%A IN ('PING %1 -n 1 -w 1 2^>NUL') DO IF "!FOD!"=="" SET FOD=%%A
  26. FOR /F "tokens=1-4 delims=." %%A IN ('ECHO.%FOD%') DO (
  27. 	IF "%%~D"=="" (
  28. 		ENDLOCAL
  29. 		GOTO Syntax
  30. 	) ELSE (
  31. 		CALL :D2H TmpA %%A
  32. 		CALL :D2H TmpB %%B
  33. 		CALL :D2H TmpC %%C
  34. 		CALL :D2H TmpD %%D
  35. 	)
  36. )
  37.  
  38. :: Display the end result
  39. ECHO.%1 = 0x%TmpA%%TmpB%%TmpC%%TmpD%
  40.  
  41. :: Done
  42. ENDLOCAL
  43. GOTO:EOF
  44.  
  45.  
  46. :D2H
  47. :: Split the number (0-255) into 2 single "digits"
  48. SET /A Tmp2 = %2 / 16
  49. SET /A Tmp1 = %2 - 16 * %Tmp2%
  50. :: Convert decimal 0-15 to single digit hexadecimal
  51. SET Cvt=0123456789ABCDEF
  52. SET Tmp1=!Cvt:~%Tmp1%,1!
  53. SET Tmp2=!Cvt:~%Tmp2%,1!
  54. :: Concatenate the 2 hexadecimal digits
  55. SET %1=%Tmp2%%Tmp1%
  56. GOTO:EOF
  57.  
  58.  
  59. :Syntax
  60. ECHO.
  61. ECHO Dec2Hex.bat,  Version 2.10 for Windows NT 4 / 2000 / XP
  62. ECHO Convert a decimal number to 8 digit hexadecimal
  63. ECHO.
  64. ECHO Usage:  DEC2HEX  number
  65. ECHO.
  66. ECHO Where:  "number" is a decimal number ranging from 0 to 4,294,967,294
  67. ECHO.
  68. ECHO         This batch file (ab)uses PING for the first step in the decimal to
  69. ECHO         hexadecimal conversion: PING splits up any decimal number into 4
  70. ECHO         2-digit hexadecimal numbers, that are then displayed as 4 decimal
  71. ECHO         numbers again. These 4 decimal numbers are then converted to 2-digit 
  72. ECHO         hexadecimal again by the rest of the batch file.
  73. ECHO         PING cannot handle 0xFFFFFFFF (DHCP broadcast address), so the highest
  74. ECHO         valid number is 4,294,967,294 (0xFFFFFFFE or 2**32 - 2).
  75. ECHO         This batch file uses FINDSTR to check the validity of the command
  76. ECHO         line argument.
  77. ECHO         For decimal numbers below 2,147,483,647 you can also use my
  78. ECHO         DecToHex.bat, which uses CMD.EXE's internal commands only.
  79. ECHO.
  80. ECHO Written by Rob van der Woude
  81. ECHO http://www.robvanderwoude.com
  82.