need help passing a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sfgman63
Posts: 8
Joined: 26 Nov 2009 01:32

need help passing a variable

#1 Post by sfgman63 » 27 Jan 2010 00:20

I'm trying to use the toHex dostips function but Im having troubles passing the variable. Here is the code:

Code: Select all

@echo off
:start
set /p dec=Enter Decimal Number to Convert :
call :toHex %dec%
echo converted decimal # = %hex%
goto start
goto :EOF

:toHex dec hex
::$created 20091203 :$changed 20091203 :$categories Arithmetic,Encoding
::$source http://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION

set /a dec=%~1
set "hex= "
SET "map=0-0;1-1;2-2;3-3;4-4;5-5;6-6;7-7;8-8;9-9;10-A;11-B;12-C;13-D;14-E;15-F"
for /L %%N in (1,1,2) do (
    set /a d=dec%%16,dec/=16
    call set h=%%map:*!d!-=%%
    set hex=!h:~0,1!!hex!
)
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" (SET %~2=%hex%) ELSE ECHO.%hex%
)
EXIT /b

When I execute the above code this is what I get:

Enter Decimal Number to Convert : 16
10
converted decimal # =

How can I get the "hex" variable to return a value outside of the toHex function?

Thanks

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: need help passing a variable

#2 Post by aGerman » 27 Jan 2010 02:32

Hi sfgman63,

you have to call the function with the name of the returned variable as 2nd parameter.
If it should be hex, then

Code: Select all

call :toHex %dec% hex


Regards
aGerman

sfgman63
Posts: 8
Joined: 26 Nov 2009 01:32

Re: need help passing a variable

#3 Post by sfgman63 » 27 Jan 2010 02:50

Hello aGerman,
Thank you very much I had a feeling it was going to be something simple that I overlooked.

rynait
Posts: 7
Joined: 28 Nov 2010 22:29

Re: need help passing a variable

#4 Post by rynait » 29 Nov 2010 19:12

got this function variable return failures.

tried variations. however is pasting the .bat file (with unimportant bodies eliminated, to make it smaller.

the problem lies with preserveswitch (colored dark blue). the idea is to swap the global variables, called by newback routine (later will be called by newactive)

i used call:evalall to echo the variables in use and see the swap has not happened.

Would be nice to figure out the function return variable.


batch script
@echo off
::variable declared or notated
set backup="K:\dwarf fortress\Active DF Genesis\data\Backup Saves\"
set active="K:\dwarf fortress\Active DF Genesis\data\save\"
set folder=%2
set newfolder=%3
set gamename="Dwarf Fortress"

::setup
call:evalall
pause
if "%3"=="" set missed=1
if "%1"=="" goto help ::originally default
if "%1"=="/c" GOTO copy
if "%1"=="/n" GOTO newback
if "%1"=="/r" GOTO newactive
if "%1"=="/h" Goto help
if "%1"=="/?" Goto help
set str=%1:~0,0
if not "%str%"=="/" GOTO swap

GOTO end

:default
::body
goto startgame

:copy
echo.this is copy
call:deltree
call:processcopy
goto startgame

:newback
echo.this is newback
call:preserveswitch %active% %backup%
if defined missed call:resolve %folder% %backup% 0
call:evalall
pause
call:processcopy
goto startgame

:newactive
echo.this is newactive
call:evalall
if defined missed call:resolve %folder% %active% 0
::md %active%.\%newfolder%
call:processcopy
GOTO startgame

:help
::body
goto end

:swap
::body
GOTO copy

preserveswitch
echo.switching
SETLOCAL
set x1=%~1
set x2=%~2
ENDLOCAL & set "%~1=%x2%"& set "%~2=%x1%"
Goto:eof


:resolve
SETLOCAL
echo.
echo.resolving
echo.
set v1=%~1
set v2=%~2
set v3=%~3
set /A str2=1
:tryagain
echo.%v2%.\%v1% %str2%
if exist "%v2%.\%v1% %str2%" (set /A str2+=1 & goto tryagain)
if "%v3%"=="1" set /A str2-=1
set v1=%v1% %str2%
echo.a folder [%v1%] is suggested
set str2=
ENDLOCAL & set newfolder=%v1%
goto:eof

:deltree
::body
goto:eof

:evalfolder
::body
goto:eof

:evalpath
:body
goto:eof

:evalall
call:evalfolder
call:evalpath
goto:eof

:processcopy
::body
goto:eof


:startgame
echo.starting game
::body

:end

end batch script....

Rynait

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: need help passing a variable

#5 Post by orange_batch » 29 Nov 2010 20:57

@sfgman63:

I know this thread is old but carlos came by with a great fast way to do DOS hex conversion. This is my slim version of his code:

Code: Select all

:: Convert 12345 to 8-digit hex. Saves to variable: %=exitcode%
cmd /d /c exit 12345

:: To remove the leading zeroes...
for /f "delims=0 tokens=*" %%x in ("%=exitcode%") do set "my_var=%%x"

echo:%=exitcode%
echo:%my_var%

You can convert any positive number from 0 to 4294967295. Just keep in mind that DOS can only do math on numbers from -2147483648 to 2147483647 (32 bits).

@rynait:

I don't understand your message well. Anyhow, don't forget syntax. If you're trying to swap the values of the variables, this is the right code:

With enabledelayedexpansion:

Code: Select all

:preserveswitch
echo.switching
SETLOCAL
set x1=!%~1!
set x2=!%~2!
ENDLOCAL & set "%~1=%x2%"& set "%~2=%x1%"
Goto:eof

Without enabledelayedexpansion:

Code: Select all

:preserveswitch
echo.switching
SETLOCAL
call set x1=%%%~1%%
call set x2=%%%~2%%
ENDLOCAL & set "%~1=%x2%"& set "%~2=%x1%"
Goto:eof

...but this is how I would do it:

Code: Select all

:swapvars
set "x=!%~1!"
set "%~1=!%~2!"
set "%~2=!x!"
exit/b

...and this is how I would do it with setlocal if I had to:

Code: Select all

:swapvars
setlocal
set "x=!%~2!"
endlocal&set "%~2=!%~1!"&set "%~1=%x%"
exit/b

rynait
Posts: 7
Joined: 28 Nov 2010 22:29

Re: need help passing a variable

#6 Post by rynait » 30 Nov 2010 00:15

tried your suggestions. It did not work.

however a question how do i find out if .bat file is set with enabledelayedexpansion?

only time setlocal is used is within the swapvars function

(yes i even changed the call:swapvars, testing the suggested code.)

does not return any error, just does not even swap the variables at all.

Roy

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: need help passing a variable

#7 Post by orange_batch » 30 Nov 2010 00:36

enabledelayedexpansion is disabled by default. It is turned on with:

Code: Select all

setlocal enabledelayedexpansion

Did you try the second script? It doesn't require delayed expansion. Unless you understand it's function, maybe stick to not using delayed expansion.

So, here's a version of my last example that doesn't require delayed expansion. Try it.

Code: Select all

:swapvars
setlocal
call set "x=%%%~2%%"
endlocal&call set "%~2=%%%~1%%"&set "%~1=%x%"
exit/b

rynait
Posts: 7
Joined: 28 Nov 2010 22:29

Re: need help passing a variable

#8 Post by rynait » 30 Nov 2010 04:37

even tried your next suggestion (same results) did not swap the variables.

I put echos in several location as debugging. seems like the bug occurr when "endlocal& set..." in the :swapvars is not carried out.

am testing with non-whitespace and shorter string.

Roy

rynait
Posts: 7
Joined: 28 Nov 2010 22:29

Re: need help passing a variable

#9 Post by rynait » 30 Nov 2010 04:43

Hello,

the test I did...

%active% is reduced to one word declaration and %backup%, two words.

(did three tests... with quote, without quotes, underline instead whitespace)

all tests failed to swap the variables.

a question, is enabledelayedexpansion supposed be in the function or in the beginning of the script?

Rynait

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: need help passing a variable

#10 Post by orange_batch » 30 Nov 2010 04:46

Either is fine. I still don't see why you have this problem... :?:

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: need help passing a variable

#11 Post by jeb » 30 Nov 2010 16:21

Hi,

you can test with this snippet, it should work

Code: Select all

@echo off
setlocal
set var1=one
set var2=this is two
call :swapvars var1 var2
echo var1=%var1%
echo var2=%var2%
goto :eof

:swapvars
setlocal
call set "x=%%%~2%%"
endlocal&call set "%~2=%%%~1%%"&set "%~1=%x%"
exit/b


Perhaps you try it like in the other expamle with

Code: Select all

call :swapvars %var1% %var2%

That variant can't work.


hope it helps
jeb

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: need help passing a variable

#12 Post by orange_batch » 30 Nov 2010 18:39

Ah, I overlooked that. Not my fault. 8)

Edit: rynait, I'll be clear. Use this syntax:

Code: Select all

call :swapvars var1 var2

with this code:

Code: Select all

:swapvars
setlocal
call set "x=%%%~2%%"
endlocal&call set "%~2=%%%~1%%"&set "%~1=%x%"
exit/b

Notice the difference?

Code: Select all

:: INCORRECT:
call :swapvars %var1% %var2%

:: CORRECT:
call :swapvars var1 var2

Post Reply