@echo off
goto :main
:strg2hex
setlocal EnableDelayedExpansion
rem Store the string in chr.tmp file
set /P "=%~1" < NUL > chr.tmp
rem Create zero.tmp file with the same number of Ascii zero characters
for %%a in (chr.tmp) do fsutil file createnew zero.tmp %%~Za > NUL
rem Compare both files with FC /B and get the differences
set "hex="
for /F "skip=1 tokens=2" %%a in ('fc /B chr.tmp zero.tmp') do set "hex=!hex!%%a"
del chr.tmp zero.tmp
echo The string to hex is %hex%
set %~1=%hex%
goto :eof
:main
echo This is the main function!
::This interprets "tiger" as a string.
::This will convert text "tiger" into hex with the above function.
call :strg2hex tiger
Echo this is text "tiger" converted into hex: %hex%
Pause
goto :eof
uj5u.com熱心網友回復:
代替
set %~1=%hex%
采用
endlocal&set "%~1=%hex%"&SET "hex=%hex%"
因為您正在執行setlocal,所以當您 時goto :eof,包含您的更改的本地環境將被洗掉,而原始的、未更改的環境將被恢復。
該陳述句之所以有效,是因為批量決議,替換然后執行命令,所以實際執行的是
endlocal&set "tiger=7469676572"&SET "hex=7469676572"
所以本地環境被洗掉,那么值是set.
@ECHO OFF
SETLOCAL
goto :main
:strg2hex
setlocal EnableDelayedExpansion
rem Store the string in chr.tmp file
set /P "=%~1" < NUL > chr.tmp
rem Create zero.tmp file with the same number of Ascii zero characters
for %%a in (chr.tmp) do DEL zero.tmp>nul&fsutil file createnew zero.tmp %%~Za > NUL
rem Compare both files with FC /B and get the differences
set "hex="
for /F "skip=1 tokens=2" %%a in ('fc /B chr.tmp zero.tmp') do set "hex=!hex!%%a"
rem del chr.tmp zero.tmp
echo The string to hex is %hex%
endlocal&SET "hex=%hex%"
goto :eof
:main
u:
echo This is the main function!
::This interprets "tiger" as a string.
::This will convert text "tiger" into hex with the above function.
call :strg2hex tiger
Echo this is text "tiger" converted into hex: %hex%
IF "%*" neq "" FOR %%z IN (%*) DO ECHO %%z& call :strg2hex %%z
rem response to comment
SET "strng=Lion"
CALL :strg2hex %strng%
ECHO This variable "strng" has been converted into %hex%
ECHO This variable "strng" value "%strng%" has been converted into %hex%
GOTO :EOF
好吧 - 更徹底地測驗的幾個驚喜......
首先,我U:用于測驗 - 這就是為什么它出現在main.
接下來,我添加了一個新行,
IF "%*" neq "" FOR %%z IN (%*) DO ECHO %%z& call :strg2hex %%z
這個看命令列“tail”(命令列中程式名后面的資料)或者引數 %*,依次賦值給每一個%%z(引數可以是空格分隔也可以是逗號分隔),然后echoes引數和calls例行公事。因此,如果我們執行
q72120539 elephant lion zebra giraffe
我們得到結果
elephant
The string to hex is 656C657068616E74
lion
The string to hex is 6C696F6E
zebra
The string to hex is 7A65627261
giraffe
The string to hex is 67697261666665
除了tiger檔案夾。q72120539是我對這個程式的名字。
我已將del 兩個臨時檔案中的一個轉換為備注,因此檔案沒有被洗掉,我可以看到它們......
不幸的是,結果顯示不是我所期望的。
盡管有其描述(或含義),但如果存在現有檔案,fsutil file createnew則不會創建新檔案 - 它保持原樣。tiger因此,在第一個名稱 ( ) 被處理后,代碼沒有更改檔案,檔案保持在 5 個位元組。
我通過在執行命令之前故意洗掉zero檔案來解決這個fsutil問題。
所以 - 現在一切都修復了,但我已經del將臨時檔案“remmed-out”,這樣在例程結束時不會被洗掉。rem 洗掉關鍵字并重新打開清理可能是一種很好的做法。
我還洗掉了 ,set "%~1=%hex%"因為將set變數命名為tiger, elephant, lion, zebra,giraffe每個都保留為它們的十六進制值字串。
編輯以包含來自 OP 的示例。
結果是額外的
This variable "strng" has been converted into 4C696F6E
This variable "strng" value "Lion" has been converted into 4C696F6E
例如,在批處理中沒有“函式”之類的東西,至少在 Pascal 中使用它的意義上是沒有的。在這種情況下,“回傳值”只是被分配給一個已知變數。
可以使用以下方法call回傳任何變數中的值:
call :function fred 5 6 7
...
:function
set /a %1=%2 %3 %4
goto :eof
這會將值設定fred為18
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/470183.html
