10進制數與33進制數的轉換
CSDN論壇,有人提出“10進制數與33進制數的轉換”,參考網上資料,經修改,得出代碼及心得如下:
一、33進制數格式
33進制數序列為:1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,V,W,X,Y,Z,0
注意字母:I、O、U不用。
二、轉換的機制
1、建立一個ASCII碼的對應表(數組),它的下標是ASCII的序列值。數組值是33進制的值。33進制轉換10進制時,靠該表由字符對應查詢到數值。
2、建立一個33進制數序列表(數組),它的下標對應33進制數值,數組值是33進制字符。10進制轉換33進制時,靠該表由1-33數值對應查詢到字符。
三、實現代碼如下:
unit Unit1;
interface
uses
Windows, Messages,SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls;
Type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Label1: TLabel;
Label2: TLabel;
PRocedure Button1Click(Sender: TObject); procedure Button2Click(Sender:TObject); procedure Button3Click(Sender:TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}const// ACIIS表 由字符對應數字 Convert: array[0..255] of Integer = (-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 33, 1, 2, 3, 4, 5, 6, 7, 8,9,-1,-1,-1,-1,-1,-1, -1,10,11,12,13,14,15,16,17,-1,18,19,20,21,22,-1, 23,24,25,26,27,-1,28,29,30,31,32,-1,-1,-1,-1,-1, -1,10,11,12,13,14,15,16,17,-1,18,19,20,21,22,-1, 23,24,25,26,27,-1,28,29,30,31,32,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);// 數字(下標)對應字符 Convert2: array[0..33] of AnsiChar = ('0','1','2','3','4','5','6','7','8','9','A', 'B','C','D','E','F','G','H','J','K','L', 'M','N','P','Q','R','S','T','V','W','X', 'Y','Z','0');function IntToHEX33(const V: Int64; constDigits: Integer = -1):string;const CSTR = '0000000000000000';var P, P1: PAnsiChar; I: Int64; NewLen: Integer;begin GetMem(P, 16); Move(CSTR, P^, 16); P1 := P + 16 - 1; I := V; while True do begin P1^ := Convert2[I mod 33]; I := I div 33; if I = 0 then Break else Dec(P1); end; NewLen := 16 - (P1 - P); if NewLen > Digits then SetString(Result, P1, NewLen) else begin P1 := P + 16 - Digits; SetString(Result, P1,Digits); end;end;function HEX33ToInt(const S: string; constDefault: Int64): Int64;var I: Integer; v: Int64;begin Result := 0; for I := 1 to length(s) do begin V := Convert[ord(s[i])]; if V < 0 then begin Result := Default; Exit; end; result := (result * 33) + V; end;end;procedure TForm1.Button1Click(Sender: TObject);begin Edit2.Text := IntToHEX33(StrToInt64Def(Edit1.Text,0));end;procedure TForm1.Button2Click(Sender: TObject);begin Edit1.Text :=IntToStr(HEX33ToInt(Edit2.Text, 0));end;procedure TForm1.Button3Click(Sender: TObject);begin close;end;
end.
四、界面
新聞熱點
疑難解答