1. English Encoding
Function encode7bit (SRC: string): string;
VaR DST: string;
I: integer;
Curs, nexts: byte;
Tstr: string;
Begin
For I: = 1 to length (SRC) Do begin
// The current character is 8th characters in 8-bit grouping, no need to encode (8 characters have been shortened to 7)
If (I mod 8) = 0 then continue;
// The current digit is a number.
Tstr: = copy (SRC, I, 1 );
Curs: = ord (tstr [1]);
// The first character in the current right shift group does not shift
If (I mod 8)> 1 then
Curs: = (curs SHR (I mod 8)-1 ));
// The last digit is a number. If the character ends
If I <length (SRC) then begin
Tstr: = copy (SRC, I + 1, 1 );
Nexts: = ord (tstr [1]);
End else
Nexts: = 0;
// Shift the last displacement to the left
Nexts: = (nexts SHL (8-(I mod 8 )));
// Convert the current shift to hexadecimal after adding the last displacement bit
DST: = DST + inttohex (curs + nexts, 2 );
End;
Result: = DST;
End;
2. English decoding
Function decode7bit (SRC: string): string;
VaR DST: string;
I: integer;
Curs, prois: byte;
Begin
For I: = 1 to (length (SRC) Div 2) Do begin
// Convert the hexadecimal format of the current digit to decimal
Curs: = strtoint ('$' + copy (SRC, (I-1) * 2 +); // 32-> 50
// Convert the hexadecimal format of the previous digit to decimal.
If (I mod 7) <> 1 then
Prois: = strtoint ('$' + copy (SRC, (I-2) * 2 + 200) // C8->
Else
Prois: = 0;
// Shifts the previous one to the right, that is, to extract the forward bits of the Current Character
If (I mod 7)> 0 then
Prois: = (prois SHR (9-(I mod 7 )))
Else
Prois: = (prois SHR 2 );
// Remove the high position from the Left shift of the Current BIT
If (I mod 7)> 0 then
Curs: = (curs SHL (I mod 7 ))
Else
Curs: = (curs SHL 7 );
// Set the eighth digit to 0
Curs: = (curs SHR 1 );
DST: = DST + CHR (curs + prois );
// The Seventh hexadecimal format contains a complete character
If (I mod 7) = 0 then begin
Curs: = strtoint ('$' + copy (SRC, (I-1) * 2 +); // 32-> 50
Curs: = (curs SHR 1 );
DST: = DST + CHR (curs );
End;
End; // en
Result: = DST;
End;
3. Chinese Encoding
Function cntousc (var s: widestring): string;
VaR
I, Len: integer;
Cur: integer;
T: string;
Begin
Result: = '';
Len: = length (s );
I: = 1;
While I <= Len do
Begin
Cur: = ord (s [I]);
// BCD Conversion
Fmtstr (T, '% 4.4x', [cur]);
Result: = Result + T;
INC (I );
End;
End;
4. PDU Decoding
Function tform1.decodechinese (SRC: string): string; // Chinese Decoding
VaR
I: integer;
S: string;
D: widechar;
Resultw: widestring;
Begin
For I: = 1 to round (length (SRC)/4) do
Begin
S: = copy (SRC, (I-1) * 4 + );
D: = widechar (strtoint ('$' + S); // This is the focus, which can be converted using the widechar provided by Delphi.
Resultw: = resultw + D;
End;
Result: = resultw;
End;