- ::测试环境Win7
- @echo off
- SetLocal EnableDelayedExpansion
- call :SetAscVar
- ::获取硬盘序列号(16进制)。可能有多块硬盘,显示中字母行“SerialNumber”,还有2行空行,需要先处理。
- for /f %%x in ('"wmic diskdrive get serialnumber|find " "|find /v /i "SerialNumber""')do (
- if not "%%x"=="" (
- set/adn+=1
- echo 硬盘!dn!:
- call :Ascii4Sn ASN %%x
- echo 16进制序列号:%%x
- echo 硬盘序列号:!ASN!
- echo\
- )
- )
- pause
-
- set sn=32535554574e4741314134313532204a20202020
- call :Ascii4Sn ASN %sn%
- echo 16进制序列号:%sn%
- echo 硬盘序列号:%ASN%
- pause
- goto :eof
-
- ::函数
- :SetAscVar //定义数字和字母的ASCII变量,需要先调用
- set Num=0123456789
- set Abc=ABCDEFGHIJKLMNOPQRSTUVWXYZ
- set Hex=%Num%%Abc:~,6%
- for /l %%i in (0 1 9)do set _0x3%%i=%%i
- for /l %%i in (0 1 26)do call :AbcAsc %%i
- goto :eof
- :AbcAsc <offset> //定义26个字母对应的ASCII变量
- set/a Asc=0x41+%1,h=Asc/16,e=Asc%%16
- set _0x!Hex:~%h%,1!!Hex:~%e%,1!=!Abc:~%1,1!
- set/a as1=0x61+%1,h=as1/16,e=as1%%16
- set _0x!Hex:~%h%,1!!Hex:~%e%,1!=!Abc:~%1,1!
- goto :eof
-
- :Ascii4Sn <str_var> <hexs> //回传变量,长串16进制数
- set hsn=%2
- set ns=
- set as=
- :ASloop
- set ns=%ns%%hsn:~2,2%%hsn:~,2%
- set as=%as%!_0x%hsn:~2,2%!!_0x%hsn:~,2%!
- set hsn=%hsn:~4%
- if not "%hsn%"=="" goto :ASloop
- set %1=%as%
- goto :eof
复制代码
|