1字节BCD码只能表示2位十进制数,你的sTmp2太长了吧?
下面的程序你看看吧:
unit MyToBCD;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,FMTBCD, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure MyStrToBCD(DString:string;j:Integer);
function MyBCDToStr(iPos:Integer;iLen:Integer):string;
var
Form1: TForm1;
arrtmp:array[0..9] of char;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var sTmp1,sTmp2,sTmp3:string;
begin
sTmp1:='123456';
sTmp2:='87651212';
sTmp3:='73469';
MyStrToBCD(sTmp1,0);
MyStrToBCD(sTmp2,3);
MyStrToBCD(sTmp3,7);
sTmp1:=MyBCDToStr(0,3);
sTmp2:=MyBCDToStr(3,4);
sTmp3:=MyBCDToSTr(7,3);
//arrtmp
sleep(5);
end;
procedure MyStrToBCD(DString:string;j:Integer);
var i:Integer;
begin
if ( (Length(DString) mod 2)=1 ) then DString:=DString+'0';
i:=1;
while i<Length(DString) do
begin
arrtmp[j]:=chr( (ord(DString) shl 4) + (ord(DString[i+1]) and $0f) );
j:=j+1;
i:=i+2;
end;
end;
function MyBCDToStr(iPos:Integer;iLen:Integer):string;
var j:Integer;
iOrd:Integer;
begin
Result:='';
for j:=iPos to iPos+iLen-1 do
begin
iOrd:=ord(arrtmp[j]);
Result:=Result + chr((iOrd shr 4)+48) + chr((iOrd and $0f)+48)
end;
end;
end.