进制转换程序示例源码

进制转换程序示例源码之一 { ##################################################################### # 声明:本站资源由Delphi编程驿站[http://www.delphidak.com]整理收集, # 部分资源来自于网络,转发前请注意尊重版权,如果您发现本站的资源 # 侵犯了您的版权,请来信告知,版主将立即删除。 # #******************** 欢迎访问Delphi编程驿站************************* # # Delphi编程驿站,以Delphi技术交流为宗旨的编程站点,明确的主题、一致的版面。 # 主页简介: # 本站的宗旨:与您共同进步、成长! # 主栏目设置:编程技巧、源码分析、组件开发、项目合作; # 辅栏目设置:网站简介、网站导航、站内更新、关于版主、友情链接。 # 在成长中学习,在学习中成长!我们一直在努力!!! # ======================刀剑如梦软件创作室============================== # ================== KingLong Software Studio ========================== # 站长:刀剑如梦 QQ:1917208 信箱:yckxzjj@163.com,yckxzjj@vip.sina.com # 网址:http://www.delphidak.com [Delphi编程驿站] # 论坛:http://www.delphibbs.com [推荐:大富翁论坛] # 资源:http://www.delphibox.com [推荐:Delphi盒子] # 资源:http://www.delphifans.com [推荐:Delphi园地] #*************** 如你转载,请不要删除以上信息,谢谢! **************** # ###################################################################### 源码名称:进制转换程序源码说明: 可以对十进制、十六进制、二进制、八进制之间相互转换数据值。 } unit RadixConvertUnit; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Radix; type TRadix = (raDec, raHex, raOct, raBin); TRadixConvertForm = class(TForm) GroupBox1: TGroupBox; Label1: TLabel; DecEdit: TEdit; Label2: TLabel; HexEdit: TEdit; Label3: TLabel; OctEdit: TEdit; Label4: TLabel; BinEdit: TEdit; OkButton: TButton; CancelButton: TButton; ClearButton: TButton; procedure CancelButtonClick(Sender: TObject); procedure OkButtonClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure DecEditEnter(Sender: TObject); procedure HexEditEnter(Sender: TObject); procedure OctEditEnter(Sender: TObject); procedure BinEditEnter(Sender: TObject); procedure ClearButtonClick(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } FFromRadix: TRadix; procedure FillText(V: Cardinal); public { Public declarations } end; var RadixConvertForm: TRadixConvertForm; procedure ShowRadixConvForm; overload; procedure ShowRadixConvForm(Value: Cardinal); overload; implementation {$R *.DFM} procedure ShowRadixConvForm; begin if RadixConvertForm <> nil then begin RadixConvertForm.WindowState := wsNormal; RadixConvertForm.BringToFront; Exit; end; RadixConvertForm := TRadixConvertForm.Create(Application); RadixConvertForm.Show; end; procedure ShowRadixConvForm(Value: Cardinal); begin if RadixConvertForm <> nil then begin RadixConvertForm.WindowState := wsNormal; RadixConvertForm.BringToFront; Exit; end; RadixConvertForm := TRadixConvertForm.Create(Application); RadixConvertForm.FillText(Value); RadixConvertForm.Show; end; procedure TRadixConvertForm.FillText(V: Cardinal); var S: string; begin DecEdit.Text := IntToStr(V); IntToStrAsHex(S, V); HexEdit.Text := S; IntToStrAsOct(S, V); OctEdit.Text := S; IntToStrAsBin(S, V); BinEdit.Text := S; end; procedure TRadixConvertForm.CancelButtonClick(Sender: TObject); begin Close; end; procedure TRadixConvertForm.OkButtonClick(Sender: TObject); var V: Cardinal; Ok: Boolean; begin Ok := False; case FFromRadix of raDec: Ok := StrToIntAsDec(V, DecEdit.Text); raHex: Ok := StrToIntAsHex(V, HexEdit.Text); raOct: Ok := StrToIntAsOct(V, OctEdit.Text); raBin: Ok := StrToIntAsBin(V, BinEdit.Text); end; if Ok then FillText(V) else begin MessageBox(Handle, '当前输入数字串中含有非法字符', '错误', 48); end; case FFromRadix of raDec: begin DecEdit.SelectAll; DecEdit.SetFocus; end; raHex: begin HexEdit.SelectAll; HexEdit.SetFocus; end; raOct: begin OctEdit.SelectAll; OctEdit.SetFocus; end; raBin: begin BinEdit.SelectAll; BinEdit.SetFocus; end; end; end; procedure TRadixConvertForm.ClearButtonClick(Sender: TObject); begin DecEdit.Text := ''; HexEdit.Text := ''; OctEdit.Text := ''; BinEdit.Text := ''; DecEdit.SetFocus; end; procedure TRadixConvertForm.FormCreate(Sender: TObject); begin FFromRadix := raDec; end; procedure TRadixConvertForm.FormClose(Sender: TObject; var Action: TCloseAction); begin Action := caFree; RadixConvertForm := nil; end; procedure TRadixConvertForm.DecEditEnter(Sender: TObject); begin FFromRadix := raDec; end; procedure TRadixConvertForm.HexEditEnter(Sender: TObject); begin FFromRadix := raHex; end; procedure TRadixConvertForm.OctEditEnter(Sender: TObject); begin FFromRadix := raOct; end; procedure TRadixConvertForm.BinEditEnter(Sender: TObject); begin FFromRadix := raBin; end; end. ============================================================== 进制转换程序示例源码之二 关键字: Radix、Convert、delphidak 分类: Delphi编程驿站源码交流 { ##################################################################### # 声明:本站资源由Delphi编程驿站[ http://www.delphidak.com ]整理收集, # 部分资源来自于网络,转发前请注意尊重版权,如果您发现本站的资源 # 侵犯了您的版权,请来信告知,版主将立即删除。 # #******************** 欢迎访问Delphi编程驿站************************* # # Delphi编程驿站,以Delphi技术交流为宗旨的编程站点,明确的主题、一致的版面。 # 主页简介: # 本站的宗旨:与您共同进步、成长! # 主栏目设置:编程技巧、源码分析、组件开发、项目合作; # 辅栏目设置:网站简介、网站导航、站内更新、关于版主、友情链接。 # 在成长中学习,在学习中成长!我们一直在努力!!! # ======================刀剑如梦软件创作室============================== # ================== KingLong Software Studio ========================== # 站长:刀剑如梦 QQ:1917208 信箱:yckxzjj@163.com,yckxzjj@vip.sina.com # 网址:http://www.delphidak.com [Delphi编程驿站] # 论坛:http://www.delphibbs.com [推荐:大富翁论坛] # 资源:http://www.delphibox.com [推荐:Delphi盒子] # 资源:http://www.delphifans.com [推荐:Delphi园地] #*************** 如你转载,请不要删除以上信息,谢谢! **************** # ###################################################################### 源码名称:进制转换程序源码说明: 可以对十进制、十六进制、二进制、八进制之间相互转换数据值。 } unit Radix; interface uses Classes, SysUtils; function StrToIntAsDec(var V: Cardinal; S: string): Boolean; forward; function StrToIntAsHex(var V: Cardinal; S: string): Boolean; forward; function StrToIntAsOct(var V: Cardinal; S: string): Boolean; forward; function StrToIntAsBin(var V: Cardinal; S: string): Boolean; forward; function IntToStrAsHex(var S: string; V: Cardinal): Boolean; forward; function IntToStrAsOct(var S: string; V: Cardinal): Boolean; forward; function IntToStrAsBin(var S: string; V: Cardinal): Boolean; forward; function IsDecStr(S: string): Boolean; forward; function IsHexStr(S: string): Boolean; forward; function IsOctStr(S: string): Boolean; forward; function IsBinStr(S: string): Boolean; forward; function IsDecChar(C: Char): Boolean; forward; function IsHexChar(C: Char): Boolean; forward; function IsOctChar(C: Char): Boolean; forward; function IsBinChar(C: Char): Boolean; forward; function IsAlphaChar(C: Char): Boolean; forward; function IsDigitChar(C: Char): Boolean; forward; implementation function StrToIntAsDec(var V: Cardinal; S: string): Boolean; begin Result := IsDecStr(S); if not Result then Exit; V := StrToInt(S); Result := True; end; function StrToIntAsHex(var V: Cardinal; S: string): Boolean; var i, j: Cardinal; begin Result := IsHexStr(S); if not Result then Exit; V := 0; S := UpperCase(S); for i := 1 to Length(S) do begin if IsDigitChar(S[i]) then j := Ord(S[i]) - Ord('0') else j := Ord(S[i]) - Ord('A') + 10; V := (V shl 4) + j; end; Result := True; end; function StrToIntAsOct(var V: Cardinal; S: string): Boolean; var i, j: Cardinal; begin Result := IsOctStr(S); if not Result then Exit; V := 0; for i := 1 to Length(S) do begin j := Ord(S[i]) - Ord('0'); V := (V shl 3) + j; end; Result := True; end; function StrToIntAsBin(var V: Cardinal; S: string): Boolean; var i, j: Cardinal; begin Result := IsBinStr(S); if not Result then Exit; V := 0; for i := 1 to Length(S) do begin j := Ord(S[i]) - Ord('0'); V := (V shl 1) + j; end; Result := True; end; function IntToStrAsHex(var S: string; V: Cardinal): Boolean; var i: Integer; begin S := ''; repeat i := V and $F; V := V shr 4; S := IntToHex(i, 1) + S; until V = 0; Result := True; end; function IntToStrAsOct(var S: string; V: Cardinal): Boolean; var i: Integer; begin S := ''; repeat i := V and $7; V := V shr 3; S := IntToStr(i) + S; until V = 0; Result := True; end; function IntToStrAsBin(var S: string; V: Cardinal): Boolean; var i: Integer; begin S := ''; repeat i := V and $1; V := V shr 1; S := IntToStr(i) + S; until V = 0; S := StringOfChar('0', 8 - Length(S)) + S; Result := True; end; function IsDecStr(S: string): Boolean; var Int: Int64; begin Result := True; try Int := StrToInt64(S); except Result := False; end; end; function IsHexStr(S: string): Boolean; var i: Integer; begin for i := 1 to Length(S) do begin if not IsHexChar(S[i]) then begin Result := False; Exit; end; end; Result := True; end; function IsOctStr(S: string): Boolean; var i: Integer; begin for i := 1 to Length(S) do begin if not IsOctChar(S[i]) then begin Result := False; Exit; end; end; Result := True; end; function IsBinStr(S: string): Boolean; var i: Integer; begin for i := 1 to Length(S) do begin if not IsBinChar(S[i]) then begin Result := False; Exit; end; end; Result := True; end; function IsDecChar(C: Char): Boolean; begin Result := (C >= '0') and (C <= '9'); end; function IsHexChar(C: Char): Boolean; begin Result := (C >= '0') and (C <= '9') or (C >= 'A') and (C <= 'F') or (C >= 'a') and (C <= 'f'); end; function IsOctChar(C: Char): Boolean; begin Result := (C >= '0') and (C <= '7'); end; function IsBinChar(C: Char): Boolean; begin Result := (C >= '0') and (C <= '1'); end; function IsAlphaChar(C: Char): Boolean; begin Result := (C >= 'A') and (C <= 'Z') or (C >= 'a') and (C <= 'z'); end; function IsDigitChar(C: Char): Boolean; begin Result := (C >= '0') and (C <= '9'); end; end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值