Powered by GeSHi

Source code for dectohex3p.bat

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

  1. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2. ::                                                                          ::
  3. ::  DecToHex.cmd                                                            ::
  4. ::                                                                          ::
  5. ::       Author: Richard K. Bussey, Copyright All Rights Reserved           ::
  6. ::        EMail: rkb@binary-construction.com                                ::
  7. ::         Date: 08.24.06                                                   ::
  8. ::                                                                          ::
  9. ::  Description: Convert a passed decimal value to its hexidecimal          ::
  10. ::               equivalent.                                                ::
  11. ::                                                                          ::
  12. ::      History:                                                            ::
  13. ::                                                                          ::
  14. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  15.  
  16. @echo off
  17. echo DecToHex.cmd v1.0
  18. echo Copyright: 2006, Richard K. Bussey; All Rights Reserved
  19. echo.
  20. setlocal
  21.  
  22. ::
  23. :: If no command line options specified go immediately to SYNTAX
  24. ::
  25. if "%1"=="" goto SYNTAX
  26.  
  27. ::
  28. :: Set Global Defaults
  29. ::
  30. set QUIET=FALSE
  31. set DEBUG=FALSE
  32. set SEPERATOR=FALSE
  33. set /a Value=-1
  34.  
  35. ::
  36. :: Parse Command Line
  37. ::
  38. :PARSE
  39. if /i "%1"=="" goto ENDPARSE
  40. if /i "%1"=="-d" (echo on & set DEBUG=TRUE)
  41. if /i "%1"=="-v" (if "%2"=="" (goto SYNTAX) else (set /a Value=%2))
  42. if /i "%1"=="-u" (set UPPER=TRUE)
  43. if /i "%1"=="-e" (if "%2"=="" (goto SYNTAX) else (set ENVVARIABLE= %~2))
  44. if /i "%1"=="-q" (set QUIET=TRUE)
  45. if /i "%1"=="-s" (set SEPERATOR=TRUE)
  46. shift
  47. goto PARSE
  48. :ENDPARSE
  49.  
  50. if "%Value%" equ "-1" @echo Value (-v n) required! & goto SYNTAX
  51. if "%DEBUG%"=="" @echo off
  52.  
  53. ::
  54. :: DOS has 32 bit precision
  55. ::
  56. set /a POSITION=8
  57. set /a OriginalValue=Value
  58.  
  59. ::
  60. :: Convert decimal values up to 268435455 (0xfffffff)
  61. ::
  62. if %Value% gtr 0xfffffff echo Value too large! & goto FINISH
  63. if %Value% gtr 0xffffff set /a Divisor=16777216 & goto LOOP
  64. if %Value% gtr 0xfffff set /a Divisor=1048576 & goto LOOP
  65. if %Value% gtr 0xffff set /a Divisor=65536 & goto LOOP
  66. if %Value% gtr 0xfff set /a Divisor=4096 & goto LOOP
  67. if %Value% gtr 0xff set /a Divisor=256 & goto LOOP
  68. if %Value% gtr 0xf set /a Divisor=16 & goto LOOP
  69. set /a Divisor=1
  70.  
  71. :LOOP
  72. if %Divisor% equ 1 goto HEX
  73. :HEX
  74. set /a POSITION=%POSITION% - 1
  75. set /a p="Value/Divisor"
  76. if %p% equ 0xf (if "%UPPER%"=="TRUE" (set p%POSITION%=F) else (set p%POSITION%=f))
  77. if %p% equ 0xe (if "%UPPER%"=="TRUE" (set p%POSITION%=E) else (set p%POSITION%=e))
  78. if %p% equ 0xd (if "%UPPER%"=="TRUE" (set p%POSITION%=D) else (set p%POSITION%=d))
  79. if %p% equ 0xc (if "%UPPER%"=="TRUE" (set p%POSITION%=C) else (set p%POSITION%=c))
  80. if %p% equ 0xb (if "%UPPER%"=="TRUE" (set p%POSITION%=B) else (set p%POSITION%=b))
  81. if %p% equ 0xa (if "%UPPER%"=="TRUE" (set p%POSITION%=A) else (set p%POSITION%=a))
  82. if %p% lss 0xa (set p%POSITION%=%p%)
  83. if %Divisor% equ 1 goto DISPLAY
  84. :CONTINUE
  85. set /a Value=%Value% - (%p% * %Divisor%)
  86. set /a Divisor=%Divisor% / 16
  87. goto LOOP
  88.  
  89. :DISPLAY
  90. if %QUIET% equ FALSE (
  91.  if %POSITION% lss 4 (
  92.   if %SEPERATOR% equ TRUE (
  93.    echo Value: %OriginalValue%; HEX: 0x%p7%%p6%%p5%:%p4%%p3%%p2%%p1%
  94.   ) else (
  95.    echo Value: %OriginalValue%; HEX: 0x%p7%%p6%%p5%%p4%%p3%%p2%%p1%
  96.   )
  97.  ) else (
  98.   if %SEPERATOR% equ TRUE (
  99.    echo Value: %OriginalValue%; HEX: 0x%p7%%p6%:%p5%%p4%
  100.   ) else (
  101.    echo Value: %OriginalValue%; HEX: 0x%p7%%p6%%p5%%p4%
  102.   )
  103.  )
  104. )
  105. if "%ENVVARIABLE%" NEQ "" (
  106.  endlocal
  107.  if %POSITION% lss 4 (
  108.   if %SEPERATOR% equ TRUE (
  109.    set %ENVVARIABLE%=0x%p7%%p6%%p5%:%p4%%p3%%p2%%p1%
  110.   ) else (
  111.    set %ENVVARIABLE%=0x%p7%%p6%%p5%%p4%%p3%%p2%%p1%
  112.   )
  113.  ) else (
  114.   if %SEPERATOR% equ TRUE (
  115.    set %ENVVARIABLE%=0x%p7%%p6%:%p5%%p4%
  116.   ) else (
  117.    set %ENVVARIABLE%=0x%p7%%p6%%p5%%p4%
  118.   )
  119.  )
  120. )
  121. goto FINISH
  122.  
  123. :SYNTAX
  124. echo DecToHex -v n [-d] [-u] [-e] [-q] [-s]
  125. echo.
  126. echo    v   Decimal value to convert; where n is the decimal value
  127. echo.
  128. echo    d   Turn debugging on (turn echo on; display all commands)
  129. echo.
  130. echo    u   Show Hex values in upper-case
  131. echo.
  132. echo    e   Save Hex value in provided Environment Variable
  133. echo.
  134. echo    q   Quiet mode (do not display results)
  135. echo.
  136. echo    s   Enable seperator (:) in Hex value
  137. echo.
  138. echo i.e. DecToHex -v 23452234 -u
  139.  
  140. :FINISH
  141.