The output might be
C:\_D\TEST>cmdfaq #ff1faa
ff 1f aa
255 31 170
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
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html
A useful application, and good example of string handling in
environment variables.
...snip
> :: Convert the hexadecimals
> set /a d1_=0x%h1_%
> set /a d2_=0x%h2_%
> set /a d3_=0x%h3_%
If you change those lines to:
:: Convert the hexadecimals
set /a d1_=0x%h1_% || GOTO _usage
set /a d2_=0x%h2_% || GOTO _usage
set /a d3_=0x%h3_% || GOTO _usage
then the error message displayed should the user enter an invalid Hex
digit is displayed along with your usage instructions, like this:
============Screen capture Windows 2000 simulated in Win95
C:\WORK>timodemo #ffffff
ff ff ff
255 255 255
C:\WORK>timodemo #fffffg
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).
+----------------------------------------------------+
| Covert a hexadecimal color code into decimal |
| By Prof. Timo Salmi, Last modified Fri 29-Apr-2005 |
+----------------------------------------------------+
Usage C:\WORK\demo.cmd #C1C2C3
Where C1 is the first color in hexadecimal and so on
C:\WORK>
============End screen capture
The "invalid number" message effectively becomes a part
of the usage instructions, but only when it's needed.
General note:
The command seperator operator || means "execute the following
command only if the previous command was successful" (= as
far as I am aware, returned non-zero ERRORLEVEL).
--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
Header email is rarely checked. Contact us at http://www.allenware.com/
> If you change those lines to: :: Convert the hexadecimals set /a
> d1_=0x%h1_% || GOTO _usage
> then the error message displayed should the user enter an invalid
> Hex
Thanks. Utilizing your welcome tip, and to make the error message
to jump out, I opt for
:: Convert the hexadecimals, check the hex input for validity
set /a d1_=0x%h1_% || (
echo. & echo Invalid hexadecimal %h1_% in %hex_%
goto :EOF)
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 script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip
I had a bash at this and derived the following:
[01]@echo off
[02]::
[03]:: Put the required hexadecimal parameter into a regular environment
variable
[04]set yh3=%1
[05]if not defined yh3 goto _usage
[06]::
[07]:: The require format of the parameter is #C1C2C2
[08]:: Where C1 is the first colour in hexadecimal and so on
[09]if not %yh3:~0,1%==# goto _usage
[10]::
[11]:: Check that the parameter is not too long...or too short
[12]set yh1=%yh3:~7,1%
[13]if defined yh1 goto _usage
[14]set yh1=%yh3:~6,1%
[15]if not defined yh1 goto _usage
[16]::
[17]:: Decompose the parameter into character-pairs
[18]set yh1=%yh3:~1,2%
[19]set yh2=%yh3:~3,2%
[20]set yh3=%yh3:~5,2%
[21]echo x|find "x">nul
[22]set /a yd1=0x%yh1% 2>nul
[23]if not errorlevel 1 set /a yd2=0x%yh2% 2>nul
[24]if not errorlevel 1 set /a yd3=0x%yh3% 2>nul
[25]if errorlevel 1 echo hex number invalid&goto cleanup
[26]::
[27]:: Display the results
[28]echo %yh1% %yh2% %yh3%
[29]echo %yd1% %yd2% %yd3%
[30]goto :EOF
[31]::
[32]:_usage
[33]echo +----------------------------------------------------+
[34]echo ^| Covert a hexadecimal colour code into decimal ^|
[35]echo ^| By Prof. Timo Salmi, Fri 29-Apr-2005 ^|
[36]echo ^| Last modified by Billous Fri 29-Apr-2005 ^|
[37]echo +----------------------------------------------------+
[38]echo.
[39]echo Usage %~f0 #C1C2C3
[40]echo.
[41]echo Where C1 is the first colour in hexadecimal and so on
[42]:cleanup
[43]for %%i in (yh1 yh2 yh3 yd1 yd2 yd3) do set %%i=
[44]echo j|find "x">nul
[45]goto :EOF
Which has the possibly-useful property of returning error-codes 0 and
non-zero in the accepted manner and also setting the returned values only if
the string supplied is a valid HEX.
Incidentally, this also worked quite happily in NT4
Philosophically, I use yXX for my varnames - it makes them really easy to
locate with a SET without the potential collision with ~z usage in a FOR (if
zXX was used) and this helps in the cleanup routine.
HTH
...Bill
(Taking it back to *.nt since my original code was posted there.)
Actually, with slight forcing mine can be condensed to a near comparable
@echo off & setlocal enableextensions
set /a d1=0x%1%
set /a d2=0x%2%
set /a d3=0x%3%
echo %d1% %d2% %d3%
endlocal & goto :EOF
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
And with a little more "forcing" your CMD.EXE version can be a one-liner,
too, Timo, if one-lining is the flavour of the day:-)
Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@echo off & setlocal & set d=& (for %%f in (%*) do (set /a p=0x%%f & set d=!d! !p!)) & echo/!d!
====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects
============Screen capture Windows 2000 simulated in Win95
C:\WORK>timodemo ff aa bb
255 170 187
C:\WORK>
============End screen capture
Above code line is not meant as a serious suggestion for use:-)
More serious note: I hadn't realised until just trying it, that (scoping brackets)
can be used to limit the entire scope of a FOR IN DO statement within a longer
line of code, the rest of which falls outside the scope of FOR IN DO repetitions.