📄 unitendecode.pas
字号:
unit UnitEnDecode;
interface
uses math,Sysutils,unitwjs,windows;
function DecodeEnglish(Input:string):string;
function EncodeEnglish(s:String):String;
function DecodeChinese(input:string):string;
function EncodeChinese(input:string):string;
function Byte2_1Xchg(var s:string):string;
function Byte4_3_2_1Xchg(var s:string):string;
function ASCII2Char(ASCII:string):string;
function StrToHex(input:string):string;
function HexToStr(input:string;xchg:boolean):string;
implementation
procedure Xchg(var c1,c2:char);
var
c:char;
begin
c:=c1;
c1:=c2;
c2:=c;
end;
function Byte2_1Xchg(var s:string):string;
var
i,len:integer;
begin
len:=length(s)-1; //2字节
i:=1;
while i<=len do
begin
Xchg(s[i],s[i+1]);
inc(i,2);
end;
result:=s;
end;
function Byte4_3_2_1Xchg(var s:string):string;
var
i,len:integer;
begin
len:=length(s)-3; //4字节
i:=1;
while i<=len do
begin
Xchg(s[i],s[i+3]);
Xchg(s[i+1],s[i+2]);
inc(i,4);
end;
result:=s;
end;
function ReturnHex(Value:integer):integer;
begin
case Value of
0:
Value:=$7f;
1:
Value:=$3f;
2:
Value:=$1f;
3:
Value:=$0f;
4:
Value:=$07;
5:
Value:=$03;
6:
Value:=$01;
7:
Value:=$00;
end;
result:=Value;
end;
function ASCII2Char(ASCII:string):string;
var //xchg=true表示每8个ASCII(4个char)反相
i,len:integer;
begin
result:='';
len:=length(ASCII);
i:=1;
while i<len do //i+1<=len
begin
result:=result+chr(strtointdef('$'+copy(ASCII,i,2),0));
inc(i,2);
end;
end;
function DecodeEnglish(Input:string):string;
const
MAXSIZE=300;
var
InStr,OutStr:array[0..MAXSIZE]of byte;
len,j,i,Point:integer;
begin
fillchar(InStr,301,0);
fillchar(OutStr,301,0);
len:=min(MAXSIZE,length(Input));
i:=1;
while i<=len do
begin
InStr[i div 2]:=StrToInt('$'+copy(Input,i,2));
inc(i,2);
end;
Point:=0;
i:=0;
j:=0;
while j<=Len div 2 do
begin
if(Point=0)then
OutStr[i]:=InStr[j] and ReturnHex(Point)
else
OutStr[i]:=((InStr[j] and ReturnHex(Point)) shl Point)
or (InStr[j-1] shr (8-Point));
if OutStr[i]=0 then OutStr[i]:=ord('@'); //wjs update
if(Point mod 7=0)and(Point<>0)then Point:=0
else inc(Point);
inc(i);
j:=i-(i div 8);
end;
//OutStr[12]:=((InStr[12] and $07) shl 5) or (InStr[11] shr (8-5));
len:=len div 2 * 8 div 7;
setlength(result,len);
move(OutStr[0],result[1],len);
end;
function DecodeChinese(input:string):string;
var //'7E85' --> '蕾'
i,len:integer;
begin
result:='';
i:=1;
len:=length(input) div 4 *4;
while i<len do
begin
result:=result+WordToHex(strtointdef('$'+copy(input,i,4),$20)); //$20是空格
inc(i,4);
end;
result:=result+WordToHex(0); //
result:=WideCharToString(PWideChar(@result[1]));
end;
function EncodeChinese(input:string):string;
var
dest:WideString;
i,len:integer;
begin //'蕾' --> '7E85'
len:=length(input);
dest:=WideString(input);
setlength(result,len*4);
result:='';
for i:=1 to length(dest) do
result:=result+format('%4.4X',[pword(@dest[i])^]);
end;
function EncodeEnglish(s:String):String;
var
i,j,len:Integer;
cur:Integer;
begin
Result:='';
len:=Length(s);
j:=0; //j 用于移位计数
i:=1;
while i<=len do
begin
if i<len then //数据变换
cur:=(ord(s[i]) shr j) or ((ord(s[i+1]) shl (7-j)) and $ff)
else
cur:=(ord(s[i]) shr j) and $7f;
result:=result+Format('%2.2X',[cur]);
inc(i);
//移位计数达到7位的特别处理
j:=(j+1) mod 7;if j=0 then inc(i);
end;
end;
function StrToHex(input:string):string;
var
i,len:integer;
begin //'AB1456' --> #$AB#$14#$56
len:=length(input);
setlength(result,len div 2);
i:=1;
while i<len do
begin
result[(i+1) div 2]:=chr(strtointdef('$'+copy(input,i,2),0));
inc(i,2);
end;
end;
function HexToStr(input:string;xchg:boolean):string;
var
len,i:integer;
function chg(b:byte):byte;
begin
result:=(b and $0F) shl 4 + (b and $F0) shr 4;
end;
begin
len:=length(input);
result:='';
for i:=1 to len do
begin
if not xchg then
result:=result+format('%.2X',[byte(input[i])])
else result:=result+format('%.2X',[chg(byte(input[i]))]);
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -