Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for dectohex.bat

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

  1. @ECHO OFF
  2. SETLOCAL ENABLEDELAYEDEXPANSION
  3. SET /A Decimal = %1 +0 >NUL 2>&1 || GOTO Syntax
  4. IF %Decimal% LSS 0 GOTO Syntax
  5.  
  6. SET Convert=0123456789ABCDEF
  7. SET Hexadecimal=
  8. SET Scratch=%Decimal%
  9.  
  10. :Loop
  11. SET /A LSB = %Scratch% %% 16
  12. SET /A "Scratch = %Scratch% >> 4"
  13. SET Hexadecimal=!Convert:~%LSB%,1!%Hexadecimal%
  14. IF NOT %Scratch% EQU 0 GOTO Loop
  15.  
  16. SET Hexadecimal=0x%Hexadecimal%
  17.  
  18. SET Decimal
  19. SET Hexadecimal
  20.  
  21. SET RC=0
  22. IF NOT %Decimal% EQU %Hexadecimal% (
  23. 	ECHO An error occurred, %Hexadecimal% is not equal to %Decimal%
  24. 	SET RC=1
  25. )
  26. ENDLOCAL & EXIT /B %RC%
  27.  
  28.  
  29. :Syntax
  30. ECHO.
  31. ECHO DecToHex.bat,  Version 2.01
  32. ECHO Convert a decimal number to "7.5" digit hexadecimal
  33. ECHO.
  34. ECHO Usage:  DECTOHEX  number
  35. ECHO.
  36. ECHO Where:  number    is a 32-bit positive integer or calculation
  37. ECHO                   (0..2,147,483,647 or 0x00000000..0x7FFFFFFF)
  38. ECHO.
  39. ECHO Notes:  This batch file uses CMD.EXE's internal commands only.
  40. ECHO         Return code ("ErrorLevel") 0 if conversion was successful,
  41. ECHO         otherwise 1.
  42. ECHO.
  43. ECHO Written by Rob van der Woude
  44. ECHO https://www.robvanderwoude.com
  45. ENDLOCAL
  46. EXIT /B 1
  47.  

page last modified: 2024-02-26; loaded in 0.0166 seconds