base128/255 help on this code
Posted: 04 Oct 2013 14:25
Hi,
I have a problem with this code in the management of "poison" character!
I not skilled for this!
Help needed , thanks.
This code get a string and convert in another base. For now 128 but the goal is higher than 128 (near the maximum which is 255 i think)
In this manner is possible (i thinks) rappresent all 256 char (or integer) in the dos batch.
If there 'a better way I'd like to know!
The initial problem is in the decode128 subroutine in the part that get the position of the character in the new "ascii" system
Einstein1969
I have a problem with this code in the management of "poison" character!
I not skilled for this!
Help needed , thanks.
This code get a string and convert in another base. For now 128 but the goal is higher than 128 (near the maximum which is 255 i think)
In this manner is possible (i thinks) rappresent all 256 char (or integer) in the dos batch.
If there 'a better way I'd like to know!
The initial problem is in the decode128 subroutine in the part that get the position of the character in the new "ascii" system
Code: Select all
@echo off & setlocal EnableDelayedExpansion & goto :Init_System
:Main
set "buffer=123456789"
call :encode128 buffer encoded
echo !encoded!
call :decode128 encoded decoded
echo !decoded!
goto :EndMain
:encode128 buffer
setlocal EnableDelayedExpansion
set buffer=!%~1!& set "ascii=!#$%()*,.0123456789:;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎ"
call :strlen Buffer size
echo "!buffer!" size:!size!
set /a size2=size+1, ls=0, rs=7, r=0
set "encoded="
set inx=0
:encode128_loop1
echo ls:!ls! inx:!inx!
if !ls! gtr 7 (
set /a inx=inx-1, ls=0, rs=7
)
rem for %%c in (buffer:~!inx!,1) set c=!%%c!
echo "!buffer!" inx:!inx!
if !inx! lss !size! (call charlib asc buffer !inx! nc) else set nc=0
set r1=nc
set /a "nc=(nc << ls), nc=(nc & 0x7F)|r, r=(r1>>rs) & 0x7F, ls=ls+1, rs-=1"
for %%c in ("ascii:~!nc!,1") do set encoded=!encoded!!%%~c!
set /a inx=inx+1
if !inx! lss !size2! goto :encode128_loop1
( endlocal
set %2=%encoded%
)
goto :eof
:decode128
setlocal EnableDelayedExpansion
rem 1 2 3 4 5 6 7 8 9 0 1 2
rem 01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
set buffer=!%~1!& set "ascii=!#$%()*,.0123456789:;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎ"
call :strlen Buffer size
echo "!buffer!" size:!size!
set /a ls=7, rs=8, r=0
set "decoded="
set inx=0
:decode128_loop1
echo ls:!ls! inx:!inx!
for %%c in ("buffer:~!inx!,1") do set c=!%%~c!
set "nc="
if "!c!"==";" set nc=20
if "!c!"=="=" set nc=21
if "!c!"=="^!" set nc=0
if not defined nc for /l %%i in (0,1,127) do (
if "!ascii:~%%i,1!"=="!c!" set /a nc=%%i
echo if "!ascii:~%%i,1!"=="!c!" set /a nc=%%i
pause
)
if not defined nc echo("!c!" not found in "!ascii!"
if !rs! gtr 7 (
set rs=1, ls=7, r=nc
) else (
set /a "r1=nc, nc=(nc<<ls) & 0xFF, nc=nc|r, r=r1>>rs, rs=rs+1, ls=ls-1;"
call charlib chr nc c
set decoded=!decoded!!c!
)
if !inx! lss !size! goto :decode128_loop1
( endlocal
set %2=%decoded%
)
goto :eof
Qui ci vanno le subroutine/fuctions
:strLen string len -- returns the length of a string
:: -- string [in] - variable name containing the string being measured for length
:: -- len [out] - variable to be used to return the string length
:: Many thanks to 'sowgtsoi', but also 'jeb' and 'amel27' dostips forum users helped making this short and efficient
:$created 20081122 :$changed 20101116 :$categories StringOperation
:$source http://www.dostips.com
( SETLOCAL ENABLEDELAYEDEXPANSION
set "str=A!%~1!"&rem keep the A up front to ensure we get the length and not the upper bound
rem it also avoids trouble in case of empty string
set "len=0"
for /L %%A in (12,-1,0) do (
set /a "len|=1<<%%A"
for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
)
)
( ENDLOCAL & REM RETURN VALUES
IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b
::_____________________________________________________________
:difftime t1var t2var result
::
:: Usage:
:: set t1=%time%
:: rem do some stuff
:: set t2=%time%
:: Call :difftime t1 t2
setlocal EnabledelayedExpansion
set "t=!%~1: =0! !%~2: =0!"
set /a c=(((1!t:~12,2!-1!t:~0,2!)*60+(1!t:~15,2!-1!t:~3,2!))*60+(1!t:~18,2!-1!t:~6,2!))*100+(1!t:~-2!-1!t:~9,2!)
if !c! lss 0 set /a c+=24*60*60*100
if "%~3" equ "" set "p=0!c!"
( endlocal & rem return value
if "%~3" neq "" (set "%~3=%c%") else ( echo %t% Elapsed: %c:~0,-2%.%p:~-2% seconds)
)
goto :eof
::_____________________________________________________________
:difftime2 t1var t2var
::
:: leggermente più lenta
setlocal EnabledelayedExpansion
for /F "tokens=1-8 delims=:.," %%a in ("!%~1: =0!,!%~2: =0!") do set /a "d=((((1%%e-1%%a)*60)+1%%f-1%%b)*60+1%%g-1%%c)*100+1%%h-1%%d
if !d! lss 0 set /a d+=24*60*60*100
set "tp=0!d!" & echo !%~1! !%~2! Elapsed: !d:~0,-2!.!tp:~-2! seconds
endlocal
goto :eof
:ReserveEnvironmentSpace sizeInKB
rem Define the first large variable (reserving 6 bytes for variable name)
set z1=X
for /L %%i in (1,1,12) do set z1=!z1!!z1!
set z1=!z1!!z1:~8!
rem Define the rest of large variables
set /A lastVar=%1 / 8
for /L %%i in (2,1,%lastVar%) do set z%%i=!z1!
rem Delete all the large variables in bottom-up order
for /L %%i in (%lastVar%,-1,1) do set z%%i=
Goto :eof
::_____________________________________________________________
:Init_System
:: Why does SET performance degrade as environment size gro
:: http://www.dostips.com/forum/viewtopic.php?f=3&t=2597&start=15
call :ReserveEnvironmentSpace 1290
:: rem per aggiungere caratteri oltre il 126 usare: alt+0128 e non alt+128
Goto :Main
::_____________________________________________________________
:EndMain
Goto :eof
Einstein1969