Powered by GeSHi

Source code for dectohex.bat

(view source code of dectohex.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. :: Enable delayed variable expansion
  16. SETLOCAL ENABLEDELAYEDEXPANSION
  17. :: Check if command line parameter is a valid number
  18. SET /A X = %1 >NUL 2>&1
  19. IF NOT "%X%"=="%~1" (
  20. 	ENDLOCAL
  21. 	GOTO Syntax
  22. )
  23. IF %1 LSS 0 (
  24. 	ENDLOCAL
  25. 	GOTO Syntax
  26. )
  27.  
  28. SET Hex=
  29. SET Cvt=0123456789ABCDEF
  30.  
  31. SET /A Tmp0 = %1
  32.  
  33. :Loop
  34. :: Get the last (least significant) digit
  35. SET /A Tmp1 = %Tmp0% / 16
  36. SET /A Tmp2 = %Tmp0% - 16 * %Tmp1%
  37. :: Convert that last digit (still decimal) to hexadecimal
  38. SET Tmp2=!Cvt:~%Tmp2%,1!
  39. :: Prepend it to the intermediate hexadecimal result
  40. SET Hex=%Tmp2%%Hex%
  41. :: Remove the last digit from the original number
  42. SET Tmp0=%Tmp1%
  43. :: If there are any digits left, loop once more
  44. IF %Tmp0% GTR 0 GOTO Loop
  45.  
  46. :: Display the end result
  47. ECHO.%1 = 0x%Hex%
  48.  
  49. :: Done
  50. ENDLOCAL
  51. GOTO:EOF
  52.  
  53.  
  54. :Syntax
  55. ECHO.
  56. ECHO DecToHex.bat,  Version 1.01 for Windows NT 4 / 2000 / XP
  57. ECHO Convert a decimal number to "7.5" digit hexadecimal
  58. ECHO.
  59. ECHO Usage:  DECTOHEX  number
  60. ECHO.
  61. ECHO Where:  "number" is a decimal number ranging from 0 to 2,147,483,647
  62. ECHO                                            (0x7FFFFFFF or 2**31 - 1)
  63. ECHO.
  64. ECHO         This batch file uses CMD.EXE's internal commands only.
  65. ECHO         For numbers up to 4,294,967,294 (0xFFFFFFFE or 2**32 - 2)
  66. ECHO         you can use my Dec2Hex.bat, which uses PING and FINDSTR.
  67. ECHO.
  68. ECHO Written by Rob van der Woude
  69. ECHO http://www.robvanderwoude.com
  70.