我对批处理文件有一些麻烦
@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set LOOKUP=0123456789abcdef &set HEXSTR=
set MAC_B1=03
set MAC_B2=5D
set MAC_B3=00
set MAC_B4=00
set MAC_B5=00
set MAC_B6=00
set my_file=runningno.txt
set runningno=128
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set LOOKUP=0123456789abcdef &set /a HEXSTR=
set /a A=%runningno%
:loop
set /a B=!A! %% 16 & set /a A=!A! / 16
set HEXSTR=!LOOKUP:~%B%,1!%HEXSTR%
if %A% GTR 0 goto :loop
if %runningno% LEQ 255 (
set /a MAC_B6 = %HEXSTR%
)
ECHO MAC address:%MAC_B1%-%MAC_B2%-%MAC_B3%-%MAC_B4%-%MAC_B5%-%MAC_B6%
如果我在没有" abcdef"
的情况下以十六进制运行,则输出正常03-5D-00-00-00-80
在其他集合runningno=255
中,输出应该是03-5D-00-00-00-FF
但是
03-5D-00-00-00-0
请帮助
答案 0 :(得分:1)
@echo off
setlocal enableextensions enabledelayedexpansion
set "mac_b1=03"
set "mac_b2=5d"
set "mac_b3=00"
set "mac_b4=00"
set "mac_b5=00"
set "mac_b6=00"
set "runningno=128"
set "lookup=0123456789abcdef"
set /a "high=(runningno & 255) >> 4", "low=runningno & 15"
set "mac_b6=!lookup:~%high%,1!!lookup:~%low%,1!"
echo %mac_b1%-%mac_b2%-%mac_b3%-%mac_b4%-%mac_b5%-%mac_b6%
已修改因为似乎runningno
中的值会增加,
@echo off
setlocal enableextensions enabledelayedexpansion
set "mac_b1=03"
set "mac_b2=5d"
set "mac_b3=00"
set "mac_b4=00"
set "mac_b5=00"
set "mac_b6=00"
set "runningno=1458796"
set "lookup=0123456789abcdef"
set "value=%runningno%"
for /l %%a in (6, -1, 3) do (
set /a "high=(value & 255) >> 4", "low=value & 15", "value>>=8"
for %%H in (!high!) do for %%L in (!low!) do (
set "mac_b%%a=!lookup:~%%H,1!!lookup:~%%L,1!"
)
)
echo %mac_b1%-%mac_b2%-%mac_b3%-%mac_b4%-%mac_b5%-%mac_b6%
但这有问题。批处理文件中的算术仅限于32位有符号整数。这意味着使用此方法可以处理的值越高0x7fffff