Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 批次:字符串和;“字符串”;不同_String_Batch File - Fatal编程技术网

String 批次:字符串和;“字符串”;不同

String 批次:字符串和;“字符串”;不同,string,batch-file,String,Batch File,我使用.bat(generate.bat)将字符串转换为base64中编码的sha256 <nul set /p =%1 | openssl dgst -sha256 -binary 2>NUL | openssl base64 2>NUL 输出为: qjV6SxGt0Ex4fN0XvaqMGbJCxFVF9ksSlLYfmtCR0G4= 但当我执行时: generate.bat "foobar" 那么输出是: w6uP8Tcg6K2QR905Rms8iXTlksL6OD

我使用.bat(generate.bat)将字符串转换为base64中编码的sha256

<nul set /p =%1 | openssl dgst -sha256 -binary 2>NUL | openssl base64 2>NUL
输出为:

qjV6SxGt0Ex4fN0XvaqMGbJCxFVF9ksSlLYfmtCR0G4=
但当我执行时:

generate.bat "foobar"
那么输出是:

w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI=
这意味着
“foobar”
foobar
不一样

为什么??有什么区别


谢谢,

由@npocmaka建议的取消引用是优雅的,应该有效

以下是另一种方法:

@echo off
setlocal

Set TXT=%1
CALL :dequote TXT

echo %TXT% | openssl dgst -sha256 -binary 2>NUL | openssl base64 2>NUL

goto xit

::  from http://ss64.com/nt/syntax-dequote.html
:DeQuote
for /f "delims=" %%A in ('echo %%%1%%') do set %1=%%~A
Goto :eof

:xit
endlocal

Windows CMD将封闭参数视为参数的一部分(字符串的一部分)

如果希望外部引号始终是字符串的一部分(即使命令行中未提供外部引号),则可以执行以下操作:

<nul set /p ^"="%~1"^" | openssl dgst -sha256 -binary 2>NUL | openssl base64 2>NUL
NUL | openssl base64 2>NUL
如果不希望在字符串中包含括引号,则通常可以执行以下操作:

<nul set /p "=%~1" | openssl dgst -sha256 -binary 2>NUL | openssl base64 2>NUL
NUL | openssl base64 2>NUL
上述策略通常有效,但如果字符串以空格或
=
开头,则用于在结尾打印字符串而不打印
\r\n
的SET/p hack不起作用

这是一个棘手的问题。我想最好的选择是切换到使用powerShell。(我不太熟悉PowerShell,所以不知道语法)


如果您真的想使用批处理,您可以使用相当复杂的过程来打印任何不带
\r\n
的字符串,如try with
NUL>openssl base64 2>NUL
-“%$1”用于删除引号每次都会给我相同的哈希值(不带
的版本相同)。
。但是我想要的是带有
的版本。那么,请明确添加引号:
接受答案,因为解释很深,谢谢
<nul set /p "=%~1" | openssl dgst -sha256 -binary 2>NUL | openssl base64 2>NUL