标题: [数值计算] 二进制换算成16进制后结果输出问题 [打印本页]
作者: smtcao008 时间: 2020-8-14 13:43 标题: 二进制换算成16进制后结果输出问题
还请大神帮忙看看,我想让任意MAC地址+1,如这个变变量%mac%为F825A3BCF203,下一个就是F825A3BCF204
我是先拆分,再计算,再合并的方式- @echo on
- echo %mac%
- echo set a=%mac:~0,8% > a.bat -----取前8位
- echo set b=%mac:~-4% > b.bat -----取后四位 F203
- call b.bat
- set /a c=0x%b%+1 备注 F203+1
- echo %c% 备注结果 61956
- set f=0123456789ABCDEF
- set num=%c%
- :a
- set /a mod = %num% %% 16
- set /a num /=16
- set en=%mod% %en%
- if not %num% lss 1 (goto a)
- for %%i in (%en%) do (call set/p=%%f:~%%i,1%%<nul)
- pause>nul
复制代码
显示结果是by 行输出单个字符,如下,
我怎么实现其输出为F204,再合并成MAC呢,求赐教,小白一枚!~~~~~~
作者: went 时间: 2020-8-14 15:04
用javascript,方便不易出错- @echo off
- set "MAC=F825A3BCF203"
- call :HexCalc %MAC% +1 result
- echo %result%
- pause&exit
-
- :HexCalc
- set "code=mshta "javascript:new ActiveXObject("Scripting.FileSystemObject").GetStandardStream(1).WriteLine((parseInt('%~1',16)%2).toString(16).toUpperCase());window.close();""
- for /f "delims=" %%i in ('%code%') do set "%~3=%%i"
- goto :eof
复制代码
作者: smtcao008 时间: 2020-8-14 15:23
回复 2# went
这个是我看不懂系列却可实行的方法,感谢大神!
作者: a20150604 时间: 2020-8-14 17:56
本帖最后由 a20150604 于 2020-8-14 18:01 编辑
纯批:- @echo off & setlocal enabledelayedexpansion
- set "MAC=F825A3BCF203"
-
- if 0x%MAC:~-6%==0xFFFFFF (
- set /a "h6=0x%MAC:~0,6% + 1"
- call :dec2hex h6 !h6!
- set "result=!h6:~-6!000000"
- ) else (
- set /a "l6=0x%MAC:~-6% + 1"
- call :dec2hex l6 !l6!
- set "result=%MAC:~0,6%!l6:~-6!"
- )
- set result
- pause
- exit
-
- :dec2hex hex dec
- setlocal
- set "dic=0123456789ABCDEF"
- for /L %%a in (0 1 7) do (
- set /a "t = ( %~2 >> (4*%%a) ) & 0xF"
- for %%p in (!t!) do set "hex_num=!dic:~%%p,1!!hex_num!"
- )
- endlocal & set "%1=%hex_num%
- exit /b
复制代码
作者: yhcfsr 时间: 2020-8-14 19:34
- powershell (0xF825A3BCF203+1).toString('X')
复制代码
作者: smtcao008 时间: 2020-8-15 14:16
回复 2# went
如果MAC前面是00开头的 输出结果就会少开头的00两位数
如MAC为 002B6341B075, +1后输出2B6341B076,可优化吗?
作者: WHY 时间: 2020-8-15 18:04
- @echo off & setlocal enabledelayedexpansion
- set "mac=002B6341B075"
- set /a x = 1
-
- for /L %%i in (6 6 12) do (
- set /a y = 0x!mac:~-%%i, 6! + x, x = y / 0x1000000
- cmd /c exit !y!
- set "result=!=ExitCode:~2!!result!"
- )
-
- echo;!result!
- pause
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |