{
模块名称:各种编码的解码
使用方法:
1、UnMimeCode(string); ///Mime编码解码
2、UnQPCode(string); ///QP编码解码
3、UnHZCode(string); ///HZ编码解码
返回值: 无
}
unit Decode;
interface
uses
Math;
function UnMimeCode(MimeString : string) : string;
function UnQPCode(QPString : string) : string;
function UnHZCode(HZString : string) : string;
implementation
function Dec2Bin(Value : integer; MaxBit : integer) : string;
begin
Result := '';
while (Value > 0) do
begin
if (Trunc(Value / 2) * 2 = Value) then
Result := '0' + Result
else
Result := '1' + Result;
Value := Trunc(Value / 2);
end;
while (Length(Result) < MaxBit) do Result := '0' + Result; //填满MaxBit位
end;
function Bin2Dec(Value : string) : integer;
var
nIndex, nLength : integer;
begin
Result := 0;
nLength := Length(Value);
for nIndex := 0 to nLength - 1 do
if (Value[nLength - nIndex] = '1') then
inc(Result, Trunc(Power(2, nIndex)));
end;
function Hex2Dec(Value : string) : integer;
var
nIndex, nLength : integer;
c : char;
begin
Result := 0;
nLength := Length(Value);
for nIndex := 0 to nLength - 1 do
begin
c := Value[nLength - nIndex];
if ((c >= 'A') and (c <= 'F')) then
inc(Result, (ord(c) - 55) * Trunc(Power(16, nIndex)))
else if ((c >= '0') and (c <= '9')) then
inc(Result, (ord(c) - 48) * Trunc(Power(16, nIndex)));
end;
end;
function UnMimeCode(MimeString : string) : string;
const
c_strBase64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; //Base64字符集
var
strBin : string;
nIndex : integer;
i : integer;
begin
Result := '';
strBin := '';
//查找Base64字符,并转换为二进制
for nIndex := 1 to Length(MimeString) do
begin
i := Pos(MimeString[nIndex], c_strBase64);
if (i > 0) then
strBin := strBin + Dec2Bin(i - 1, 6) //填满6位,满足Base64编码原则
else if (MimeString[nIndex] = '=') then //无输入字符时候,使用等号输出(这样的写法应该是错误的,但目前想不出好的写法)
strBin := strBin + '000000';
end;
//转换为8位长的字符
for nIndex := 1 to Trunc(Length(strBin) / 8) do
begin
Result := Result + Chr(Bin2Dec(Copy(strBin, (nIndex - 1) * 8 + 1, 8)));
end;
end;
function UnQPCode(QPString : string) : string;
var
nIndex, nLength : integer;
begin
Result := '';
nIndex := 1;
nLength := Length(QPString);
while (nIndex <= nLength) do
begin
if (QPString[nIndex] = '=') and
(nIndex + 2 <= nLength) and
(((QPString[nIndex + 1] >= 'A') and (QPString[nIndex + 1] <= 'F')) or ((QPString[nIndex + 1] >= '0') and (QPString[nIndex + 1] <= '9'))) and
(((QPString[nIndex + 2] >= 'A') and (QPString[nIndex + 2] <= 'F')) or ((QPString[nIndex + 2] >= '0') and (QPString[nIndex + 2] <= '9'))) then
begin
Result := Result + Chr(Hex2Dec(Copy(QPString, nIndex + 1, 2)));
inc(nIndex, 3);
end
else
begin
Result := Result + QPString[nIndex];
inc(nIndex);
end;
end;
end;
function UnHZCode(HZString : string) : string;
var
nBeginIndex, nEndIndex : integer;
s, s1, strBin : string;
nIndex : integer;
begin
Result := HZString;
//查找编码字串标志
nBeginIndex := Pos('~{', Result);
nEndIndex := Pos('~}', Result);
while ((nBeginIndex > 0) and (nBeginIndex < nEndIndex)) do
begin
s := Copy(Result, nBeginIndex + 2, nEndIndex - nBeginIndex - 2);
s1 := '';
for nIndex := 1 to Length(s) do
begin
if (ord(s[nIndex]) <= 127) then
begin
strBin := Dec2Bin(ord(s[nIndex]), 8); //填满8位,满足HZ编码原则
strBin[1] := '1'; //最高位置1
s1 := s1 + Chr(Bin2Dec(strBin));
end;
end;
//替换原来的编码字串
Delete(Result, nBeginIndex, nEndIndex - nBeginIndex + 2);
Insert(s1, Result, nBeginIndex);
//查找编码字串标志
nBeginIndex := Pos('~{', Result);
nEndIndex := Pos('~}', Result);
end;
end;
end.