概述
87654321-->
00654321
00000087
87654321-->
00432100
00000087
00008765
87654321-->
00210000
00008765
00876543
00876543 00218765 00432187 00654321
1:
00654321-->
00654321
2:
00432187-->
87000000
00654321
00004321
3:
00218765-->
87650000
00004321
00000021
4:
00876543-->
87654300
00000021
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Math;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function Encode6BitBuf(const XSource; XSrcLen: Integer; var XDest; XDestLen: Integer): Integer;
var
LPSrc, LPDest: System.PByte;
I: Integer;
LRestCount: Integer;
LBtMade: Byte;
LBtCh: Byte;
LBtRest: Byte;
begin
LPSrc := System.PByte(@XSource);
LPDest := System.PByte(@XDest);
LRestCount := 0;
LBtRest := 0;
Result := 0;
for I := 0 to XSrcLen - 1 do
begin
if Result >= XDestLen then
Break;
LBtCh := LPSrc^;
LBtMade := Byte((LBtRest or (((LBtCh shl (2 + LRestCount)) and $FF) shr 2)) and $3F);
LBtRest := Byte((LBtCh shr (8 - (2 + LRestCount))) and $3F);
Inc(LRestCount, 2);
if LRestCount < 6 then
begin
LPDest^ := LBtMade + $3C;
Inc(LPDest);
Inc(Result);
end else
begin
if Result < XDestLen - 1 then
begin
LPDest^ := LBtMade + $3C;
Inc(LPDest);
LPDest^ := LBtRest + $3C;
Inc(LPDest);
Inc(Result, 2);
end else
begin
LPDest^ := LBtMade + $3C;
Inc(LPDest);
Inc(Result);
end;
LRestCount := 0;
LBtRest := 0;
end;
Inc(LPSrc);
end;
if LRestCount > 0 then
begin
LPDest^ := LBtRest + $3C;
Inc(LPDest);
Inc(Result);
end;
LPDest^ := 0;
end;
function Decode6BitBuf(const XSource; XSrcLen: Integer; var XDest; XDestLen: Integer): Integer;
var
LPSrc, LPDest: System.PByte;
I, nBitPos, nMadeBit: Integer;
btCh, btTmp, btByte: Byte;
begin
LPSrc := System.PByte(@XSource);
LPDest := System.PByte(@XDest);
nBitPos := 2;
nMadeBit := 0;
Result := 0;
btTmp := 0;
for I := 0 to XSrcLen - 1 do
begin
if (LPSrc^ < $3C) or (LPSrc^ > $7B) then
begin
Result := 0;
Break;
end;
btCh := LPSrc^ - $3C;
if Result >= XDestLen then
Break;
if (nMadeBit + 6) >= 8 then
begin
btByte := Byte(btTmp or (((btCh and $3F) shl (8 - nBitPos)) and $FF));
LPDest^ := btByte;
Inc(LPDest);
Inc(Result);
nMadeBit := 0;
if nBitPos < 6 then
Inc(nBitPos, 2)
else
begin
nBitPos := 2;
Inc(LPSrc);
Continue;
end;
end;
btTmp := Byte(btCh shr (nBitPos - 2));
Inc(nMadeBit, 8 - nBitPos);
Inc(LPSrc);
end;
LPDest^ := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
LStr: string;
LEnStr: string;
I: Integer;
LLen: Integer;
//bt: System.Byte;
begin
SetLength(LStr, 26);
for I := 1 to Length(LStr) do
begin
LStr[I] := Char(Ord('A') + I - 1);
end;
//bt := $FF; 左移8位 Byte 如果不转成Byte, $FF --> $FF0
//ShowMessage(IntToStr(Byte(bt shl 2) shr 2));
SetLength(LEnStr, Ceil(Length(LStr) * 8 / 6));
LLen := Encode6BitBuf(LStr[1], Length(LStr), LEnStr[1], Length(LEnStr));
FillChar(LStr[1], Length(LStr), 0);
LLen := Decode6BitBuf(LEnStr[1], LLen, LStr[1], Length(LStr));
if LLen <> Length(LStr) then
SetLength(LStr, LLen);
ShowMessage(LStr);
end;
end.
|
最后
以上就是怡然鼠标为你收集整理的Delphi 6Bit 编码的全部内容,希望文章能够帮你解决Delphi 6Bit 编码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复