Post an example of encoding and decoding, which has great limitations. Because I don't know how to convert the decoded numbers back to the original characters. This example simply determines a few character numbers by looking up a table, and Chinese characters are not possible. Also, I don't know how to handle special characters, so there will be many errors when there are special characters (of course, when there are special characters).
If using echo when generating a file, it can reduce some errors when inputting special characters, but it still has to judge the two numbers 0D0A (carriage return and line feed), whatever.
::code by 0401
@echo off
setlocal
set ASCII= !"#$%%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz{|}~
setlocal enabledelayedexpansion
set B64CODE=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=
set str=
set/p "str=Please enter the string to be encoded: "
if not defined str exit/b
call :enc
call :dec
exit/b
:enc
rem The two file names for comparison are:
rem Put the string in a file for comparison (fc) and find the length of the string
set/p=%str%<nul>.str
for %%f in (.str) do set strlen=%%~zf
echo The length of the original string is: %strlen% bytes
rem Generate the file for fc to compare with the string
for /l %%l in (0,1,%strlen%) do >>.cmp set/p= <nul
rem Store the 16进制 of the compared string into a variable
for /f "tokens=3" %%i in ('fc/b .cmp .str^|find "A1"') do (set strhex=!strhex!%%i)
del .cmp .str
echo The converted hexadecimal is: %strhex%
:encloop
if not defined strhex goto :encbreak
set chr1=0x!strhex:~0,2!
set chr2=0x!strhex:~2,2!
set chr3=0x!strhex:~4,2!
set strhex=%strhex:~6%
for %%i in (chr1 chr2 chr3) do if "!%%i!"=="0x" set %%i=0
set/a enc1=%chr1%">>"2
set/a enc2=((%chr1%"&"3)"<<"4)"|"(%chr2%">>"4)
set/a enc3=((%chr2%"&"15)"<<"2)"|"(%chr3%">>"6)
set/a enc4=%chr3%"&"63
::echo The shifted data: %enc1% %enc2% %enc3% %enc4%
if %chr3% equ 0 if %chr2% equ 0 (set enc4=64& set enc3=64) else (set enc4=64)
::echo The shifted data is encoded as:!B64CODE:~%enc1%,1!!B64CODE:~%enc2%,1!!B64CODE:~%enc3%,1!!B64CODE:~%enc4%,1!
set encstr=!encstr!!B64CODE:~%enc1%,1!!B64CODE:~%enc2%,1!!B64CODE:~%enc3%,1!!B64CODE:~%enc4%,1!
goto :encloop
:encbreak
echo The string encoded data is
goto :eof
:dec
rem And find the length of the encoded string
set/p=%encstr%<nul>.str
for %%f in (.str) do set strlen=%%~zf
echo The length of the encoded string is: %strlen% bytes
del .str
rem Find the number of offsets during encoding from the encoded string encstr
set str=
for /l %%l in (0,1,%strlen%) do (
for /l %%m in (0,1,64) do (
if "!encstr:~%%l,1!"=="!B64CODE:~%%m,1!" (
set str=!str!/%%m
)
)
)
set a=1&set b=4
:decloop
if %strlen% equ 0 goto :decbreak
for /f "tokens=%a%-%b% delims=/" %%i in ("%str%") do set enc1=%%i&set enc2=%%j&set enc3=%%k&set enc4=%%l
::echo The offsets of the 4 bytes are respectively: %enc1% %enc2% %enc3% %enc4%
set/a a+=4&set/a b+=4
set/a strlen-=4
set/a chr1=((%enc1%"<<"2)"|"(%enc2%">>"4))-32
set/a chr2=(((%enc2%"&"15)"<<"4)"|"(%enc3%">>"2))-32
set/a chr3=(((%enc3%"&"3)"<<"6)"|"%enc4%)-32
::echo The decimal after decoding the 4 bytes and subtracting 32 is: %chr1% %chr2% %chr3%
if %enc4% equ 64 if %enc3% equ 64 (set chr3=107&set chr2=107) else (set chr3=107)
::echo The decoded data is:!ASCII:~%chr1%,1!!ASCII:~%chr2%,1!!ASCII:~%chr3%,1!
set decstr=!decstr!!ASCII:~%chr1%,1!!ASCII:~%chr2%,1!!ASCII:~%chr3%,1!
goto :decloop
:decbreak
echo The encoded data decoded is
goto :eof
Recent Ratings for This Post
( 2 in total)
Click for details