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

📄 unit1.pas

📁 用delphi进行进制转换的例子
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    CheckBox1: TCheckBox;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    CheckBox2: TCheckBox;
    procedure Button1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
//二进制字符转十进制
Function binToDec(Value :string) : string;//integer;
VAR
 str : String;
 Int : Integer;
 i : integer;
BEGIN
 Str := UpperCase(Value);
 Int := 0;
 FOR i := 1 TO Length(str) DO
  Int := Int * 2+ ORD(str[i]) - 48;
 Result := IntToStr(Int);
end;

//十进制转二进制 函数一
function mod_num(n1,n2:integer):integer;//取余数
begin
 result:=n1-n1 div n2*n2
end;
//十进制转二进制 函数二
function reverse(s:String):String;//取反串
Var
 i,num:Integer;
 st:String;
begin
 num:=Length(s);
 st:='';
 For i:=num DownTo 1 do
 Begin
  st:=st+s[i];
 End;
 Result:=st;
end;
//十进制转化二进制
Function DecTobin(Value :Integer) : string;
Var
 ST:String;
 N:Integer;
Begin
 ST:='';
 n:=value;
 While n>=2 Do
 Begin
  st:=st+IntToStr(mod_num(n,2));
  n:=n div 2;
 End;
 st:=st+IntToStr(n);
 Result:=reverse(st);
End;

procedure TForm1.Button1Click(Sender: TObject);
var
 sj,bz,kh,je,rq,ye,jy,jy1,jy2:string;
 kh_l:integer;
begin
 sj:=uppercase(edit1.Text);
if length(sj)=24 then
begin
 //标志
 bz:=copy(sj,1,2);
 if (checkbox1.Checked=true) then
 begin
  if (bz='00') then  bz:='食堂粮食';
  if (bz='80') then  bz:='食堂加款机粮食';
  if (bz='89') then  bz:='食堂加款机现金';
  if (bz='09') then  bz:='食堂现金';
 end else
 begin
  if (bz='80') then  bz:='餐饮加款机粮食';
  if (bz='00') then  bz:='餐饮现金';
 end;
 //卡号
 kh:=copy(sj,3,4);
 kh:='$'+kh;
 kh:=inttostr(strtoint(kh));
 kh_l:=length(kh);
 while kh_l<6 do
 begin
  kh:='0'+kh;
  kh_l:=kh_l+1;
 end;
 //消费额
 je:=copy(sj,7,4)+copy(sj,12,1);
 je:=copy(je,5,1)+copy(je,3,2)+'.'+copy(je,1,2);
 je:=floattostr(strtofloat(je))+'元';
 //日期
 rq:=copy(sj,11,1)+copy(sj,13,4);
 rq:='$'+rq;
 rq:=dectobin(strtoint(rq));
 while length(rq)<20 do rq:='0'+rq;
 rq:=bintodec(copy(rq,1,4))+'月'+bintodec(copy(rq,5,5))+'日'+bintodec(copy(rq,10,5))+'时'+bintodec(copy(rq,15,6))+'分';
 //余额
 ye:=copy(sj,17,6);
 ye:=copy(ye,5,2)+copy(ye,3,2)+'.'+copy(ye,1,2);
 ye:=floattostr(strtofloat(ye))+'元';
 //校验
 jy1:=copy(sj,23,2);
 jy2:=format('%x',[strtoint('$FFFFFF')-
               (strtoint('$'+copy(sj,3,2))+
               strtoint('$'+copy(sj,5,2))+
               strtoint('$'+copy(sj,7,2))+
               strtoint('$'+copy(sj,9,2))+
               strtoint('$'+copy(sj,11,2))+
               strtoint('$'+copy(sj,13,2))+
               strtoint('$'+copy(sj,15,2))+
               strtoint('$'+copy(sj,17,2))+
               strtoint('$'+copy(sj,19,2))+
               strtoint('$'+copy(sj,21,2)))]);
 jy2:=copy(jy2,5,2);
 if jy1=jy2 then jy:='检验值正确' else jy:='检验值错误';

 label1.Caption := bz;
 label2.Caption := kh;
 label3.Caption := je;
 label4.Caption := rq;
 label5.Caption := ye;
 label6.Caption := jy;
end else showmessage('数值不够');
 edit1.SetFocus;
end;

procedure TForm1.CheckBox2Click(Sender: TObject);
begin
 if checkbox2.Checked then
  self.FormStyle := fsStayOnTop
 else
  self.FormStyle := fsNormal;
end;

end.

⌨️ 快捷键说明

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