[How to] dec to hex and hex to dec conversion bat
Posted: 25 Sep 2011 18:34
I spent entire day searching the way how to convert dec value to hex and back, unfortunately there is no documented way to do it, but there are 2 tricks, which i want to share to help other people avoid googling all day long without success (well success was partial, there was a big messy code which i refused to use):
Dec to hex
the trick is exit code variable, which i found by brute force %=exitcode% in mix with exit command, this is how it works
Note - %dec%=%errorlevel% in this case!
This is it - somehow, when you use dec value to set errorlevel, this %=exitcode% variable mirrors errolevel value in hex.
You may use substrings like this %=exitcode:~-4,-2% (uses 4 last symbols except last 2).
call cmd /c used to prevent quiting your current script (try to use just exit and see for yourself).
I also tried to call for blank function like this:
but for some reason exitcode appears unset when time comes to set hex value.
Hex to Dec
This trick is simpler and works with set /a like this
whole magic is in 0x (zero x) prefix, set /a consider everything behind 0x as hex value.
0x5=0x05=0x0005...0x00000005 so you should not really care about leading zeroes. but you can strip them.
I personally using these tricks to convert dec variable in hex and then search and replace them in binary with help of this hex editing tool XVI32 http://www.chmaas.handshake.de/delphi/f ... /xvi32.htm
and this simple script for it, which could be executed from bat with arguments as sets of hex or dec values:
You may also find useful this trick i use to change hex byte order, since most of binary data in Windows written in reversed byte order :
1A 2B 3C 4D >> 4D 3C 2B 1A
Dec to hex
the trick is exit code variable, which i found by brute force %=exitcode% in mix with exit command, this is how it works
Code: Select all
call cmd /c exit /b %dec%
set hex=%=exitcode%
Note - %dec%=%errorlevel% in this case!
This is it - somehow, when you use dec value to set errorlevel, this %=exitcode% variable mirrors errolevel value in hex.
You may use substrings like this %=exitcode:~-4,-2% (uses 4 last symbols except last 2).
call cmd /c used to prevent quiting your current script (try to use just exit and see for yourself).
I also tried to call for blank function like this:
Code: Select all
call dectohex %dec%
set hex=%=exitcode%
exit
:dectohex
exit /b %1
but for some reason exitcode appears unset when time comes to set hex value.
Hex to Dec
This trick is simpler and works with set /a like this
set /a dec=0x%hex%
whole magic is in 0x (zero x) prefix, set /a consider everything behind 0x as hex value.
0x5=0x05=0x0005...0x00000005 so you should not really care about leading zeroes. but you can strip them.
I personally using these tricks to convert dec variable in hex and then search and replace them in binary with help of this hex editing tool XVI32 http://www.chmaas.handshake.de/delphi/f ... /xvi32.htm
and this simple script for it, which could be executed from bat with arguments as sets of hex or dec values:
Code: Select all
REPLACE %1 BY %2
EXIT
You may also find useful this trick i use to change hex byte order, since most of binary data in Windows written in reversed byte order :
Code: Select all
call cmd /c exit /b %dec%
set hex=%=exitcode%
SET hexa=!hex:~-2!
SET hexb=!hex:~-4,-2!
SET hexc=!hex:~-6,-4!
set hexd=!hex:~-8,-6!
set xeh=!hexa!!hexb!hexc!!hexd!
1A 2B 3C 4D >> 4D 3C 2B 1A