Wow!! that is really funky cool bananas!
Well done.
In fact, submit it to RosettaCode <http://www.rosettacode.org> !
Bruce
>Hello, I devised a quick way to convert decimal numbers to hexadecimal
>using undocumented =ExitCode variable.
>
>
>@echo off
>setlocal enableextensions
>call :10to16 1789 ret
>echo.%ret%
>pause
>goto :eof
>
>:10to16:
>setlocal enableextensions
>set "findstr=%WinDir%\system32\findstr.exe"
>"%ComSpec%" /a /q /d /e:on /c exit /b %~1 >nul
>for /f "tokens=2 delims==" %%# in (
>'set "" ^| "%findstr%" /r "^\=ExitCode\="'
>) do for /f "eol=0 delims=0 tokens=*" %%$ in (
>"%%#") do endlocal & set "%2=%%$"
>goto :eof
Nice work carlos.
This does the same, with simpler syntax.
@echo off
call :Dec2Hex 6789 ret
echo.%ret%
pause
goto :eof
:Dec2Hex
"%ComSpec%" /c exit /b %~1 >nul
for /f "tokens=1* delims==0" %%a in (
'set "" ^|find "=ExitCode"') do set "%2=%%b"
--
Regards,
Mic
That's nice work too. However, you get some pretty strange results for
certain values. Using this version
@echo off
setlocal
call :Dec2Hex %1 ret
echo.%ret%
REM ~ pause
goto :eof
:Dec2Hex
"%ComSpec%" /c exit /b %~1 >nul
for /f "tokens=1* delims==0" %%a in ('set "" ^|find "=ExitCode"') do set
"%2=%%b"
You get the following
>hex2dec.cmd 32
>hex2dec.cmd 33
!
>hex2dec.cmd 34
"
>hex2dec.cmd 35
#
>hex2dec.cmd 36
$
>hex2dec.cmd 37
%
>hex2dec.cmd 38
>hex2dec.cmd 39
'
>hex2dec.cmd 40
(
>hex2dec.cmd 44
,
>hex2dec.cmd 46
.
>hex2dec.cmd 48
>hex2dec.cmd 50
2
>hex2dec.cmd 55
7
>hex2dec.cmd 60
The syntax of the command is incorrect.
>hex2dec.cmd 61
>hex2dec.cmd 63
?
>hex2dec.cmd 70
F
>hex2dec.cmd 80
P
Kind regards,
Bruce.
@echo off
setlocal enableextensions
call :10to16 %1 ret
echo.%ret%
REM ~ pause
goto :eof
:10to16:
setlocal enableextensions
set "findstr=%WinDir%\system32\findstr.exe"
"%ComSpec%" /a /q /d /e:on /c exit /b %~1 >nul
for /f "tokens=2 delims==" %%# in ('set "" ^| "%findstr%" /r
"^\=ExitCode\="') do for /f "eol=0 delims=0 tokens=*" %%$ in ("%%#") do
endlocal & set "%2=%%$"
goto :eof
gives the right answers
-- Bruce
:: h2d_demo.cmd
@echo off
setlocal enableextensions
set ret=carlos:
for /L %%i in (1,1,100) do call :carlos %%i
echo.%ret%
set ret=foxidrive:
for /L %%i in (1,1,100) do call :foxidrive %%i
echo.%ret%
REM ~ pause
goto :eof
:carlos
setlocal enableextensions
set "findstr=%WinDir%\system32\findstr.exe"
"%ComSpec%" /a /q /d /e:on /c exit /b %~1 >nul
for /f "tokens=2 delims==" %%# in ('set "" ^| "%findstr%" /r
"^\=ExitCode\="') do for /f "eol=0 delims=0 tokens=*" %%$ in ("%%#") do
endlocal & set "ret=%ret% %%$"
goto :eof
:foxidrive
"%ComSpec%" /c exit /b %~1 >nul
for /f "tokens=1* delims==0" %%a in ('set "" ^|find "=ExitCode"') do set
"ret=%ret% %%b"
foxidrive's code never runs to completion.
-- Bruce
'set "" ^|findstr "=ExitCode\="'
then everything works fine.
-- Bruce
Thanks Bruce, I see there is an "=ExitCodeAscii=" variable as well.
This works here:
@echo off
for /L %%a in (1,1,100) do call :Dec2Hex %%a ret
pause
goto :eof
:Dec2Hex
"%ComSpec%" /c exit /b %~1 >nul
for /f "tokens=1* delims==0" %%a in (
'set "" ^|findstr "^=ExitCode="') do set "%2=%%b"
echo.%ret%
--
Regards,
Mic
@echo off
setlocal
1>nul reg add hkcu\test /v hex /t reg_dword /d %~1 /f
for /f "tokens=3" %%i in ('reg query hkcu\test /v hex') do (
set "hex=%%i"
)
1>nul reg delete hkcu\test /v hex /f
echo %hex%
That's insane! Positively, marvellously, insane!
nice job Carlos
@01MDM: Here's variant:
----------------------------------------------8<----------------------------------------------
for /f "tokens=1,3" %%i in ('^(
reg add hkcu\console /v $hex /t reg_dword /d 36564558 /f ^&
reg query hkcu\console /v $hex ^&
reg delete hkcu\console /v $hex /f
^)^|find "REG_DWORD"') do Set %%i=%%j
Set $hex
------------------------------------------8<-----------------------------------------------
Funky! Pity there's no error trapping. I took your code and make it more
tool-like:
@echo off
setlocal
if "%1" equ "" goto :eof
for /f "tokens=1,3" %%i in ('^( reg add hkcu\console /v $hex /t
reg_dword /d %1 /f ^& reg query hkcu\console /v $hex ^& reg delete
hkcu\console /v $hex /f ^)^|find "REG_DWORD"') do Set %%i=%%j
echo %$hex%
then I pushed it a bit:
>dec2hex.cmd 45
0x2d
>dec2hex.cmd
>dec2hex.cmd 55555555
0x34fb5e3
>dec2hex.cmd 555555555
0x211d1ae3
>dec2hex.cmd 5555555555
ERROR: Invalid syntax. Specify valid numeric value for '/d'.
Type "REG ADD /?" for usage.
ERROR: The system was unable to find the specified registry key or value.
ERROR: The system was unable to find the specified registry key or value.
ECHO is off.
>
----------------------------------------------8<----------------------------------------------
@echo off
setlocal ENABLEDELAYEDEXPANSION
set hex=0123456789abcdef
set num=%1
:loop
set /a digit = num %% 16
set /a num = num / 16
call :hexify %digit% res
if %num% equ 0 goto :done
goto loop
:hexify
set chunk=!hex:~%1,1!
set name=%2
set %name%=%chunk%!%name%!
goto :eof
:done
echo %res%
----------------------------------------------8<----------------------------------------------
Bruce
> ... some pretty strange results for certain values.
> @echo off
> setlocal
> call :Dec2Hex %1 ret
> echo.%ret%
> REM ~ pause
> goto :eof
>
> :Dec2Hex
> "%ComSpec%" /c exit /b %~1 >nul
> for /f "tokens=1* delims==0" %%a in ('set "" ^|find "=ExitCode"') do
> set "%2=%%b"
>
> You get the following
>
> >hex2dec.cmd 32
>
>
> >hex2dec.cmd 33
> !
>
> >hex2dec.cmd 34
> "
This too is useful! You are getting the ASCII character for the given
value! My Windows Vista HOBOs doesn't seem to have the variable
"=ExitCodeASCII" so I can't experiment with this procedure.
Frank
Thank you Carlos -- this may be the tool of the year!
Frank
There are plenty of alternatives, both practical and esoteric, including
http://www.netikka.net/tsneti/info/tscmd088.htm
All the best, Timo
--
Prof. Timo Salmi mailto:t...@uwasa.fi ftp & http://garbo.uwasa.fi/
Hpage: http://www.uwasa.fi/laskentatoimi/english/personnel/salmitimo/
Department of Accounting and Finance, University of Vaasa, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.php
> There are plenty of alternatives, both practical and esoteric, including
> http://www.netikka.net/tsneti/info/tscmd088.htm
The easiest solution, however, being
@echo off & setlocal enableextensions
set vbscalc_=%temp_%\vbscalc.vbs
echo WScript.Echo Eval("%*")>"%vbscalc_%"
cscript //nologo "%vbscalc_%"
for %%f in ("%vbscalc_%") do if exist %%f del %%f
endlocal & goto :EOF
Taken from http://www.netikka.net/tsneti/info/tscmd061.htm
The output could be e.g.
C:\_D\TEST>CMDFAQ.CMD Hex(123)
7B
But you have to admit that carlos' approach is a little truer to the
topics of this group. Plus, when written in its most compact form it
takes no more lines of code than the hybrid version ...
@echo off & setlocal enableextensions
if [%2]==[] %0 %1 ret % default variable name %
"%ComSpec%" /c exit /b %~1
for /f "tokens=1* delims=0" %%a in (
"x0%=exitcode%") do endlocal & set "%2=0%%a%%b"
call echo.%2=%%%2%%
Note that I added a 0x prefix to make it a 'true' hex number. That
is, it can be used in an arithmetic SET expression as a number. This
routine also takes an optional variable name as its second command
line argument.
_____________________
Tom Lavedas
Thanks Frank. About ExitCodeAscii, this is only seted when you exit
with number between 32 and 126, this return ascii character, example
if I do exit /b 90 ExitCodeAscii is 'Z'
I want to emphasize that my last version, is more fast than Timo vbs
version. Test with timethis.exe indicate that my code delay 0,078 and
timo vbs 0,109.
This is my final version:
@echo off
call :Dec2Hex 10554896 ret
echo.%ret%
pause
goto :eof
:Dec2Hex
> But you have to admit that carlos' approach is a little truer to the
> topics of this group.
Certainly. Inventive, interesting, and esoteric.
> Plus, when written in its most compact form it
> takes no more lines of code than the hybrid version ...
I'm not competing. Besides, once again, the more, the merrier.
All the best, Timo
--
Prof. Timo Salmi mailto:t...@uwasa.fi ftp & http://garbo.uwasa.fi/
Kind regards,
Bruce.
------------------------------------8<-----------------------------
[1] set $hex1=0x0
[2] for /f "tokens=1* delims=0=" %%a in ('"%comspec% /cexit/b
%1&set;|
find "^=ExitCode^=""') do Set $hex1=0x%%b
[3] goto :eof
[cmd] c:\> carlos.cmd 125
0x7D
----------------------------------8<---------------------------------
to see =ExitCodeAsi variable
----------------------8<----------------------------------
%comspec% /cexit/b 103&set;|find "=ExitCode"
-----------------------8<----------------------------------------
> The easiest solution, however, being
>
> @echo off & setlocal enableextensions
> set vbscalc_=%temp_%\vbscalc.vbs
> echo WScript.Echo Eval("%*")>"%vbscalc_%"
> cscript //nologo "%vbscalc_%"
> for %%f in ("%vbscalc_%") do if exist %%f del %%f
> endlocal & goto :EOF
Seems to me this would fail because %temp_% is not defined.
--
My girlfriend said "Its me or that computer"
God - I'm gonna miss her
>> set vbscalc_=%temp_%\vbscalc.vbs
> Seems to me this would fail because %temp_% is not defined.
Kindly test before making brave assertions. The script will just go to
the root of the current partition if %temp_% is undefined. Or, remove
the _ if it disturbs you.
> On 28.04.2010 17:47 Klaatu <kla...@nospam.invalid> wrote:
>> On Tue, 27 Apr 2010 17:12:16 GMT, Timo Salmi posted to
>> alt.msdos.batch.nt:
>
>>> set vbscalc_=%temp_%\vbscalc.vbs
>
>> Seems to me this would fail because %temp_% is not defined.
>
> Kindly test before making brave assertions. The script will just go to
> the root of the current partition if %temp_% is undefined. Or, remove
> the _ if it disturbs you.
Kindly admit to an error when you make one, or ignore my comment if it
disturbs you. :P
Perhaps "fail" was the wrong word though. "Fail to perform as intended"
would be better, as obviously the intent is for the file to be placed
into a (if not the) "temp" folder.
--
A journey of a thousand miles begins with a cash advance.
From that I still have to suspect that you did not try out the code
before your claim.
> or ignore my comment if it disturbs you. :P
An attempt at humor always is welcome, even when the premise falters.
> Perhaps "fail" was the wrong word though. "Fail to perform as intended"
> would be better, as obviously the intent is for the file to be placed
> into a (if not the) "temp" folder.
You are mispresenting the task at hand by an irrelevant point which does
not even hold. The original purpose was to solve the problem stated in
the subject. The code does that. Furthermore, I told where the code was
extracted from. Would you have looked at the item, you would have seen
that there temp_ is defined. The fact, however, remains that the code
that I posted works whether it is defined or not.
I readily admit when I make error and they are pointed out. I often make
errors. But this was not the case this time. But I can sympathize. A
failed one-upmanship tends particularly to rankle when one thinks one
had caught a regular.
All the best, Timo
--
Prof. Timo Salmi mailto:t...@uwasa.fi ftp & http://garbo.uwasa.fi/
Home page: http://www.uwasa.fi/laskentatoimi/henkilokunta/salmitimo/
Department of Accounting and Finance, University of Vaasa, Finland
Timo's FAQ materials at http://lipas.uwasa.fi/~ts/http/tsfaq.html
Then it is on my Windows Vista machine; my previous experimentation with
the '=' variables was with Windows NT4.
Even with it being only ASCII032 through ASCII126 it is still useful in
many ways. One is to easily write difficult characters to a variable,
such as '!' when delayed expansion is enabled:
"%ComSpec%" /d /c Exit 33
Set "char=!=ExitCodeAscii!"
Echo ASCII033="!char!"
Displays:
ASCII033="!"
Frank
It's not necessary if you set the variable before hand, and can be
displayed outside double quotes as a bonus.
@echo off
Set "char=!"
setlocal EnableDelayedExpansion
Echo ASCII033=!char!abc!char!
pause
--
Regards,
Mic
Yes: "if". But the state of delayed expansion is normally not known; it
can be tested and disabled if necessary but it is simpler to use the
'CMD/CEXIT' trick.
Frank