@echo off
::interactive mode
if "%1" equ "" (
if not defined run goto:interactive
goto:error
)
::null results
for %%i in ("x0" "0x0") do if "%1" equ "%%~i" goto:null
::parsing arguments
setlocal enabledelayedexpansion
::only one argument should input
set "arg=0"
for %%i in (%*) do set /a "arg+=1"
if "%arg%" neq "1" if not defined run goto:help
if "%arg%" neq "1" if defined run goto:error
::get length of argument
set "i=0"
set "str=%1"
for /l %%i in (0, 1, 255) do (
set "chr=!str:~%%i!"
if defined chr set /a "i+=1"
)
::checking first n symbols
if !i! equ 1 (
if "%str%" equ "0" goto:null
if "%str%" equ "x" goto:error
)
if !i! equ 2 (
if "%str:~0,1%" equ "0" goto:error
if "%str:~0,1%" equ "x" set "str=0%str%" && goto:check
)
if !i! gtr 2 (
if "%str:~0,1%" equ "0" if "%str:~1,1%" neq "x" goto:error
if "%str:~0,1%" equ "0" if "%str:~1,1%" equ "x" goto:hex2dec
if "%str:~0,1%" equ "x" set "str=0%str%" && goto:check
)
for %%i in (a b c d e f) do if "%str:~0,1%" equ "%%i" set "str=0x%str%" && goto:hex2dec
:check
2>nul set /a "res=%str%"
if "%errorlevel%" equ "0" (
if "%str%" equ "%res%" goto:dec2hex
goto:hex2dec
)
echo "%str%" | findstr /r [0-9a-f] > nul
if "%errorlevel%" equ "0" set "str=0x%str%" && goto:hex2dec
goto:error
:dec2hex
set "map=0123456789ABCDEF"
for /l %%i in (1, 1, 8) do (
set /a "res=str & 15, str >>=4"
for %%j in (!res!) do set "hex=!map:~%%j,1!!hex!"
)
for /f "tokens=* delims=0" %%i in ("!hex!") do set "hex=0x%%i"
echo %1 = !hex! & goto:eof
:hex2dec
2>nul set /a "res=%str%"
if "%errorlevel%" gtr "0" goto:error
for /f "tokens=2,3" %%i in ('findstr "# " "%~dpnx0"') do set "str=!str:%%i=%%j!"
if "%res%" neq "" if "%res%" neq "0" (echo %str% = %res%) else goto:error
endlocal
exit /b
:error
echo =^>err
exit /b 1
:null
echo =^>nil
exit /b 0
:help
::Hex2dec v2.01 - converts hex to decimal and vice versa
::Copyright (C) 2012-2013 greg zakharov
::
::Usage: hex2dec [decimal | hexademical]
::
::Example 1:
:: C:\>hex2dec 0x017a
:: 0x017A = 378
::
::Example 2:
:: C:\>hex2dec 017a
:: =>err
::
::Example 3:
:: C:\>hex2dec x017a
:: 0x017A = 378
::
::Example 4:
:: C:\>hex2dec 17a
:: 0x17A = 378
::
::Example 5:
:: C:\>hex2dec 0503
:: =>err
::
::Example 6:
:: C:\>hex2dec x503
:: 0x503 = 1283
::
::Example 7:
:: C:\>hex2dec 1283
:: 1283 = 0x503
::
::Example 8:
:: C:\>hex2dec 503
:: 503 = 0x1F7
::
::Note: hex2dec starts with interactive mode if there is
::no argument.
for /f "tokens=* delims=:" %%i in ('findstr "^::" "%~dpnx0"') do echo.%%i
exit /b 0
rem :: Upper case chart ::
# a A
# b B
# c C
# d D
# e E
# f F
rem :: End of chart ::
:interactive
::interactive mode on
echo Hex2dec v2.01 - converts hex to decimal and vice versa
echo.
echo Enter decimal or hexademical number and press Enter to
echo take result. Use "exit" or "clear" commands to quit or
echo to make host clear.
echo.
setlocal
::already launched marker
set "run=true"
:begin
set /p "ask=>>> "
cmd /c "%~dpnx0" %ask%
if "%ask%" equ "clear" cls
if "%ask%" equ "exit" cls & goto:eof
echo.
goto:begin
endlocal
exit /b
Diff to Previous Revision
--- revision 4 2012-11-14 19:08:52
+++ revision 5 2013-02-20 15:29:12
@@ -1,82 +1,114 @@
@echo off
- ::unequal symbols
- for %%i in ("!", "x") do if "%1" equ "%%~i" goto:error
- ::display help information args
- for %%i in ("-h", "/h", "-help", "/help") do (
- if "%1" equ "%%~i" if not defined run goto:help
- )
- if "%1" equ "-?" if not defined run goto:help
- if "%1" equ "/?" if not defined run goto:help
- ::interactive mode
- if "%1" equ "" (
- if not defined run goto:interactive
- goto:error
- )
- ::parsing input data
- setlocal enabledelayedexpansion
- ::verify arguments length
- set "args=0"
- for %%i in (%*) do set /a "args+=1"
- if "%args%" neq "1" if not defined run goto:help
- if "%args%" neq "1" if defined run goto:error
- ::checking is arg hex or dec
- 2>nul set /a "res=%1"
- if "%1" equ "%res%" goto:dec2hex
- ::patterns
- echo "%1" | findstr /r \x > nul
- set e1=%errorlevel%
- echo "%1" | findstr /r [0-9a-f] > nul
- set e2=%errorlevel%
- echo "%1" | findstr /r [g-wyz] > nul
- set e3=%errorlevel%
- ::debugging info section
- rem # echo %e1% %e2% %e3%
- ::parsing error codes
- if %e1% equ 0 if %e2% equ 0 if %e3% equ 1 set "k=%1" & goto:hex2dec
- if %e1% equ 1 if %e2% equ 0 if %e3% equ 1 set "k=0x%1" & goto:hex2dec
- goto:error
+ ::interactive mode
+ if "%1" equ "" (
+ if not defined run goto:interactive
+ goto:error
+ )
+ ::null results
+ for %%i in ("x0" "0x0") do if "%1" equ "%%~i" goto:null
+ ::parsing arguments
+ setlocal enabledelayedexpansion
+ ::only one argument should input
+ set "arg=0"
+ for %%i in (%*) do set /a "arg+=1"
+ if "%arg%" neq "1" if not defined run goto:help
+ if "%arg%" neq "1" if defined run goto:error
- :dec2hex
- 2>nul set /a "num=%1"
- set "map=0123456789ABCDEF"
- for /l %%i in (1, 1, 8) do (
- set /a "res=num & 15, num >>=4"
- for %%j in (!res!) do set "hex=!map:~%%j,1!!hex!"
- )
- for /f "tokens=* delims=0" %%i in ("!hex!") do set "hex=0x%%i"
- echo %1 = !hex! & goto:eof
+ ::get length of argument
+ set "i=0"
+ set "str=%1"
+ for /l %%i in (0, 1, 255) do (
+ set "chr=!str:~%%i!"
+ if defined chr set /a "i+=1"
+ )
+ ::checking first n symbols
+ if !i! equ 1 (
+ if "%str%" equ "0" goto:null
+ if "%str%" equ "x" goto:error
+ )
+ if !i! equ 2 (
+ if "%str:~0,1%" equ "0" goto:error
+ if "%str:~0,1%" equ "x" set "str=0%str%" && goto:check
+ )
+ if !i! gtr 2 (
+ if "%str:~0,1%" equ "0" if "%str:~1,1%" neq "x" goto:error
+ if "%str:~0,1%" equ "0" if "%str:~1,1%" equ "x" goto:hex2dec
+ if "%str:~0,1%" equ "x" set "str=0%str%" && goto:check
+ )
+ for %%i in (a b c d e f) do if "%str:~0,1%" equ "%%i" set "str=0x%str%" && goto:hex2dec
- :hex2dec
- set "num=%k%"
- if "%num:~0,1%" equ "x" goto:error
- 2>nul set /a "res=%k%"
- for /f "tokens=2,3" %%i in ('findstr "# " "%~dpnx0"') do set "num=!num:%%i=%%j!"
- if "%res%" neq "" (echo !num! = !res!) else goto:error
- endlocal
+ :check
+ 2>nul set /a "res=%str%"
+ if "%errorlevel%" equ "0" (
+ if "%str%" equ "%res%" goto:dec2hex
+ goto:hex2dec
+ )
+ echo "%str%" | findstr /r [0-9a-f] > nul
+ if "%errorlevel%" equ "0" set "str=0x%str%" && goto:hex2dec
+ goto:error
+
+ :dec2hex
+ set "map=0123456789ABCDEF"
+ for /l %%i in (1, 1, 8) do (
+ set /a "res=str & 15, str >>=4"
+ for %%j in (!res!) do set "hex=!map:~%%j,1!!hex!"
+ )
+ for /f "tokens=* delims=0" %%i in ("!hex!") do set "hex=0x%%i"
+ echo %1 = !hex! & goto:eof
+
+ :hex2dec
+ 2>nul set /a "res=%str%"
+ if "%errorlevel%" gtr "0" goto:error
+ for /f "tokens=2,3" %%i in ('findstr "# " "%~dpnx0"') do set "str=!str:%%i=%%j!"
+ if "%res%" neq "" if "%res%" neq "0" (echo %str% = %res%) else goto:error
+ endlocal
exit /b
:error
- if defined run echo =^>err & goto:eof
- echo Invalid data.
+ echo =^>err
exit /b 1
+:null
+ echo =^>nil
+exit /b 0
+
:help
-::Hex2dec v1.11 - converts hex to decimal and vice versa
-::Copyright (C) 2012 Greg Zakharov
+::Hex2dec v2.01 - converts hex to decimal and vice versa
+::Copyright (C) 2012-2013 greg zakharov
::
-::Usage: hex2dec [decimal | hexadecimal]
+::Usage: hex2dec [decimal | hexademical]
::
::Example 1:
:: C:\>hex2dec 0x017a
:: 0x017A = 378
::
::Example 2:
-:: C:\>hex2dec 13550
-:: 13550 = 0x34EE
+:: C:\>hex2dec 017a
+:: =>err
::
::Example 3:
-:: C:\>hex2dec 23f
-:: 0x23F = 575
+:: C:\>hex2dec x017a
+:: 0x017A = 378
+::
+::Example 4:
+:: C:\>hex2dec 17a
+:: 0x17A = 378
+::
+::Example 5:
+:: C:\>hex2dec 0503
+:: =>err
+::
+::Example 6:
+:: C:\>hex2dec x503
+:: 0x503 = 1283
+::
+::Example 7:
+:: C:\>hex2dec 1283
+:: 1283 = 0x503
+::
+::Example 8:
+:: C:\>hex2dec 503
+:: 503 = 0x1F7
::
::Note: hex2dec starts with interactive mode if there is
::no argument.
@@ -93,22 +125,22 @@
rem :: End of chart ::
:interactive
- ::interactive mode on
- echo Hex2dec v1.11 - converts hex to decimal and vice versa
- echo.
- echo Enter decimal or hexademical number and press Enter to
- echo take result. Use "exit" or "clear" commands to quit or
- echo to make host clear.
- echo.
- setlocal
- ::already launched marker
- set "run=true"
- :begin
- set /p "ask=>>> "
- cmd /c "%~dpnx0" %ask%
- if "%ask%" equ "clear" cls
- if "%ask%" equ "exit" cls & goto:eof
+ ::interactive mode on
+ echo Hex2dec v2.01 - converts hex to decimal and vice versa
echo.
- goto:begin
- endlocal
+ echo Enter decimal or hexademical number and press Enter to
+ echo take result. Use "exit" or "clear" commands to quit or
+ echo to make host clear.
+ echo.
+ setlocal
+ ::already launched marker
+ set "run=true"
+ :begin
+ set /p "ask=>>> "
+ cmd /c "%~dpnx0" %ask%
+ if "%ask%" equ "clear" cls
+ if "%ask%" equ "exit" cls & goto:eof
+ echo.
+ goto:begin
+ endlocal
exit /b