⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 decode.pas

📁 电子小说阅读器v2.68可用于阅读pdf.html等各种形式的文档
💻 PAS
字号:
{
模块名称:各种编码的解码

使用方法:1、UnMimeCode(string);
		  2、UnQPCode(string);
		  3、UnHZCode(string);

返回值:  无
}

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.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -