150. Can I convert DEC to HEX in a batch file, and vice versa?
==============================================================
This can be done e.g. with G(nu)AWK or QBASIC. The gawk solutions
are given below. No error checking for the correct input format of
the digits is included in the conversion algorithms.
@echo off
rem HEX to DEC
::
if "%1"=="" goto _usage
::
> tmp$$$.awk echo BEGIN{
>> tmp$$$.awk echo k=1
>> tmp$$$.awk echo d=0
>> tmp$$$.awk echo base=16
>> tmp$$$.awk echo number="%1"
>> tmp$$$.awk echo i=length(number)+1
>> tmp$$$.awk echo while(i!=1)
>> tmp$$$.awk echo {
>> tmp$$$.awk echo i--
>> tmp$$$.awk echo j=index("123456789ABCDEF",substr(number,i,1))
>> tmp$$$.awk echo d=d+j*k
>> tmp$$$.awk echo k=base*k
>> tmp$$$.awk echo }
>> tmp$$$.awk echo printf"%%s in HEX is %%s in DEC\n",number,d
>> tmp$$$.awk echo }
::
gawk -f tmp$$$.awk
::
for %%f in (tmp$$$.awk) do if exist %%f del %%f
goto _out
::
:_usage
echo.
echo Usage: %0 HEX to be converted to DEC
echo.
::
:_out
As is seen the above can be easily generalized by changing the
"base" variable. However, from DEC to HEX gawk inbuilt features can
be used as a simple shortcut:
@echo off
rem DEC to HEX
::
if "%1"=="" goto _usage
::
gawk 'BEGIN{printf"%%X\n","%1"}'
goto _out
::
:_usage
echo.
echo Usage: %0 DEC to be converted to HEX
echo.
::
:_out
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful batch files and tricks ftp://garbo.uwasa.fi/pc/link/tsbat.zip
>150. Can I convert DEC to HEX in a batch file, and vice versa?
Can also be done with NOWMINUS, using f16, T, $, # - range 0 to 2^31-1;
bases 10, 16, 36 interconvert.
Can also be done with LONGCALC, range 0 to about 10^65520, all bases in
2..16. For bigger numbers, request the Delphi compilation.
NOWMINUS f16 $ t7fffffff # r
-> 2147483647
LONGCALC 16 bas 07fffffffffffffff 10 bas wrt | COLS &3 1-
-> +9,223,372,036,854,775,807
I wonder what #149 will be!
--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.
> I wonder what #149 will be!
It will be
149) A batch to compare which of two files is the more recent?
Still apropos conversions, here is another one
@echo off
rem DEC to BIN
::
if "%1"=="" goto _usage
::
> tmp$$$.awk echo BEGIN{
>> tmp$$$.awk echo number=%1
>> tmp$$$.awk echo base=2
>> tmp$$$.awk echo digit="0123456789ABCDEF"
>> tmp$$$.awk echo result=""
>> tmp$$$.awk echo while(number!=0)
>> tmp$$$.awk echo {
>> tmp$$$.awk echo i=number-base*int(number/base)
>> tmp$$$.awk echo result=substr(digit,i+1,1)result
>> tmp$$$.awk echo number=int(number/base)
>> tmp$$$.awk echo }
>> tmp$$$.awk echo printf"@set bin_=%%s\n",result
>> tmp$$$.awk echo }
::
gawk -f tmp$$$.awk > tmp$$$.bat
::
for %%c in (call del) do %%c tmp$$$.bat
echo %1 DEC = %bin_% BIN
set bin_=
for %%f in (tmp$$$.awk) do if exist %%f del %%f
goto _out
::
:_usage
echo.
echo Usage: %0 DEC to be converted to BIN
NOWMINUS Oz2.$$$ R ] Oz2.htm R - F$1000 Ediff L0 Esign
->
2002-08-23T10:59:10
2002-08-23T11:17:16
NOWMINUS: Environ Status = 0 OK=TRUE diff=-0000001086
NOWMINUS: Environ Status = 0 OK=TRUE sign=-
effectively SET DIFF=+0000000000 for same age
otherwise SET SIGN= either + or - for which is older
The range of difference is a longint of seconds.
--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.txt
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.