2

In a Windows cmd script (aka bat script), I have a FOR /L loop from 1 to 8, where I need to do a bit shift and somehow format a variable as a hexadecimal number (which if you ask, is a single CPU identifier bit to feed into /AFFINITY).

I can't figure out how to do the last step. This is my loop.cmd file:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %%i IN (1,1,8) DO (
    SET /A "J=1<<%%i"
    ECHO %%i and !J!
)

which does everything but format a hex number:

1 and 2
2 and 4
3 and 8
4 and 16
5 and 32
6 and 64
7 and 128
8 and 256

expected output is:

1 and 2
2 and 4
3 and 8
4 and 10
5 and 20
6 and 40
7 and 80
8 and 100

How do you format a hexadecimal number?

Community
  • 1
  • 1
Mike T
  • 41,085
  • 18
  • 152
  • 203
  • If you are stuck using only batch, there is a solution posted to http://stackoverflow.com/questions/5737658/how-do-i-display-integers-in-hexadecimal-from-a-batch-file – Brian Tiffin Oct 06 '15 at 04:07
  • You can find a simple solution for decimal-to-hexadecimal number conversion there: [hexadecimal to decimal batch file](http://stackoverflow.com/a/33027661)... – aschipfl Jun 23 '16 at 23:59

2 Answers2

3
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %%i IN (1,1,8) DO (
    SET /A "J=1<<%%i"
    CALL :DECTOHEX J
    ECHO %%i and !J!
)
GOTO :EOF

:DECTOHEX VAR
SET "DEC=!%1!"
SET "HEX="
:NEXT
    SET /A DIGIT=DEC%%16, DEC/=16
    SET "HEX=%DIGIT%%HEX%"
IF %DEC% NEQ 0 GOTO NEXT
SET "%1=%HEX%"
EXIT /B

EDIT: Reply to the comment

Previous solution works correctly when the shifted value have just one bit on, as stated in the question. If the shifted value may have several bits on then a more general decimal-to-hexadecimal conversion is required, like the one below:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

REM DEFINE THE HEXA DIGITS
SET "HEXA=0123456789ABCDEF"

FOR /L %%i IN (1,1,8) DO (
    SET /A "J=3<<%%i"
    CALL :DECTOHEX J
    ECHO %%i and !J!
)
GOTO :EOF

:DECTOHEX VAR
SET "DEC=!%1!"
SET "HEX="
:NEXT
    SET /A DIGIT=DEC%%16, DEC/=16
    SET "HEX=!HEXA:~%DIGIT%,1!%HEX%"
IF %DEC% NEQ 0 GOTO NEXT
SET "%1=%HEX%"
EXIT /B
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • While it works for the example in the question, it does not properly convert to hex, e.g. I'm trying a new bitmask for two processors with `SET /A "J=3<<%%i"` where base-10 192 should be hex c0, and not hex 120 using the current DECTOHEX method. – Mike T Oct 06 '15 at 22:22
  • Of course. This solution was written based on the original request: a value with _one bit on_ that is shifted to the left. If the shifted value may have _several bits on_, why you didn't stated this point from the very beginning? **`:(`**. See the edit in my answer... – Aacini Oct 07 '15 at 00:25
0
@echo off
setlocal enabledelayedexpansion
set x=2
set n=1
set /a result=n
for /l %%a in (1,1,10) do (
  set /a result*=x
  if "!result:~0,1!"=="1" set result=!result:16=10!
  echo %%a and !result!
)

output:

1 and 2
2 and 4
3 and 8
4 and 10
5 and 20
6 and 40
7 and 80
8 and 100
9 and 200
10 and 400
Paul
  • 2,620
  • 2
  • 17
  • 27