cascading-style cascading-style - 4 months ago 22
Apache Configuration Question

How to generate random Hex String from batch file?

Friends, I am trying to generate a random hex string on my web server. I am using Apache HTTPD on Windows 10. I am using the hex string for session ID's etc.

The code I have tried so far:

setlocal enabledelayedexpansion
set HESSTR=0123456789ABCDEF
set /a tempone="("%RANDOM%"*"16")"
set /a RANHEXS=tempone"/"32767
echo %RANHEXS%
set hexout=!HESSTR:~%RANHEXS%,1!
echo %hexout%

But this code Always returns
... What am I doing wrong?

Thanks a lot!

@Set /a num=%random% %% 16 + 1 
Echo %num% / 16

See set /?. % is defined in the C language as

% The result of the remainder operator is the remainder when the first operand is divided by the second

And we need to escape the % with another %, so %%.

So you divide a number by the range you want - so 165 %% 16 = 5 (the remainder). Then we add 1 to make it 1 to 16 else it would be 0 to 15.