我正在编写一个批处理脚本文件(.bat),我需要在其中解码base64(适用于Windows的Base64实用程序)文本并将它们编码为十六进制。我可以做这个。问题是我需要将结果存储在变量中。
例如:
我在cmd中执行:
base64 -d -s ZGG01YZq3ZJLbK3Cz1nOhA |十六进制转储
打印到标准输出的结果是:
000000 64 61 b4 d5 86 6a dd 92 4b 6c ad c2 cf 59 ce 84
000010 01 b4 d5 86 6a dd 92 4b 6c ad c2 cf 59 ce
我只需要存储第一行,没有任何空格,也没有前缀 000000。要清楚的是,我需要将 6461b4d5866add924b6cadc2cf59ce84 存储在变量 readValue1 中。
我正在尝试使用 .bat 文件中的这段代码,但它崩溃了:
call :decodefunction
:decodefunction
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
for /f "delims=" %%a IN ('base64 -d -s ZGG01YZq3ZJLbK3Cz1nOhA | hexdump') DO (
SET readValue!count!=%%a
SET /a count=!count!+1
)
ECHO %readValue1%
ECHO %readValue2%
ENDLOCAL
此代码满足您的要求:
:decodefunction
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "tokens=1*" %%a IN ('base64 -d -s ZGG01YZq3ZJLbK3Cz1nOhA | hexdump') DO (
SET "readValue1=%%b"
GOTO break
)
:break
SET "readValue1=%readValue1: =%"
ECHO %readValue1%
ENDLOCAL