做了一个 62 进制的简单实现
添加时间:2013-7-4 点击量:
62 进制须要的字符及次序: 0..9 a..z A..Z; 只实现了 62 进制字符串与整数的互换.
unit H62;
interface
uses SysUtils;
function IntToH62(N: UInt64): string; //整数转到 62 进制字符串
function H62ToInt(S: string): UInt64; //62 进制字符串转到整数
implementation
function _C2B(C: Char): Byte; inline;
begin
Result := 0;
if CharInSet(C, [0..9]) then Exit(Byte(C) - 48); //0..9
if CharInSet(C, [a..z]) then Exit(Byte(C) - 97 + 10); //a..z
if CharInSet(C, [A..Z]) then Exit(Byte(C) - 65 + 36); //A..Z
end;
function _B2C(B: Byte): Char; inline;
begin
if B <= 9 then Exit(Char(B + 48)); //0..9
if (B >= 10) and (B <= 35) then Exit(Char(B - 10 + 97)); //a..z
if (B >= 36) and (B <= 61) then Exit(Char(B - 36 + 65)); //A..Z
end;
function _PowerN(B,P: Cardinal): UInt64; inline;
var
i: Integer;
begin
Result := B;
for i := 1 to P-1 do Result := Result B;
end;
function _C2V(C: Char; N: Byte): UInt64; inline;
begin
Result := 0;
if (N = 0) then Exit(_C2B(C));
if (N > 0) then Result := _C2B(C) _PowerN(62, N);
end;
function IntToH62(N: UInt64): string;
var
C: Char;
begin
Result := ;
repeat
C := _B2C(N mod 62);
Result := C + Result;
N := N div 62;
until (N = 0);
end;
function H62ToInt(S: string): UInt64;
var
C: Char;
L,N,I: Cardinal;
begin
Result := 0;
L := Length(S);
if L > 11 then raise Exception.Create(Err: H62ToInt); //不克不及多于 11 位
for I := L downto 1 do
begin
C := S[I];
N := L - I;
Result := Result + _C2V(C, N);
end;
end;
end.
//测试:
uses H62;
procedure TForm1.FormCreate(Sender: TObject);
var
n: Cardinal;
I: UInt64;
str: string;
begin
str := IntToH62(MaxInt); // 2lkCB1
n := H62ToInt(str); // 2147483647
I := 9999999999999999999; // 19 位
str := IntToH62(I); // bUI6zOLZTrh
str := ZZZZZZZZZZZ; // 最大值
I := H62ToInt(str); // 15143072536417990655; 比 UInt64 的最大值(18446744073709551615)小一点, 比 Int64 的最大值(9223372036854775807)大一点
end;
读书,不要想着实用,更不要有功利心。读书只为了自身的修养。邂逅一本好书如同邂逅一位知己,邂逅一个完美之人。有时心生敬意,有时怦然心动。仿佛你心底埋藏多年的话,作者替你说了出来,你们在时光深处倾心相遇的一瞬间,情投意合,心旷神怡。
62 进制须要的字符及次序: 0..9 a..z A..Z; 只实现了 62 进制字符串与整数的互换.
unit H62;
interface
uses SysUtils;
function IntToH62(N: UInt64): string; //整数转到 62 进制字符串
function H62ToInt(S: string): UInt64; //62 进制字符串转到整数
implementation
function _C2B(C: Char): Byte; inline;
begin
Result := 0;
if CharInSet(C, [0..9]) then Exit(Byte(C) - 48); //0..9
if CharInSet(C, [a..z]) then Exit(Byte(C) - 97 + 10); //a..z
if CharInSet(C, [A..Z]) then Exit(Byte(C) - 65 + 36); //A..Z
end;
function _B2C(B: Byte): Char; inline;
begin
if B <= 9 then Exit(Char(B + 48)); //0..9
if (B >= 10) and (B <= 35) then Exit(Char(B - 10 + 97)); //a..z
if (B >= 36) and (B <= 61) then Exit(Char(B - 36 + 65)); //A..Z
end;
function _PowerN(B,P: Cardinal): UInt64; inline;
var
i: Integer;
begin
Result := B;
for i := 1 to P-1 do Result := Result B;
end;
function _C2V(C: Char; N: Byte): UInt64; inline;
begin
Result := 0;
if (N = 0) then Exit(_C2B(C));
if (N > 0) then Result := _C2B(C) _PowerN(62, N);
end;
function IntToH62(N: UInt64): string;
var
C: Char;
begin
Result := ;
repeat
C := _B2C(N mod 62);
Result := C + Result;
N := N div 62;
until (N = 0);
end;
function H62ToInt(S: string): UInt64;
var
C: Char;
L,N,I: Cardinal;
begin
Result := 0;
L := Length(S);
if L > 11 then raise Exception.Create(Err: H62ToInt); //不克不及多于 11 位
for I := L downto 1 do
begin
C := S[I];
N := L - I;
Result := Result + _C2V(C, N);
end;
end;
end.
//测试:
uses H62;
procedure TForm1.FormCreate(Sender: TObject);
var
n: Cardinal;
I: UInt64;
str: string;
begin
str := IntToH62(MaxInt); // 2lkCB1
n := H62ToInt(str); // 2147483647
I := 9999999999999999999; // 19 位
str := IntToH62(I); // bUI6zOLZTrh
str := ZZZZZZZZZZZ; // 最大值
I := H62ToInt(str); // 15143072536417990655; 比 UInt64 的最大值(18446744073709551615)小一点, 比 Int64 的最大值(9223372036854775807)大一点
end;
读书,不要想着实用,更不要有功利心。读书只为了自身的修养。邂逅一本好书如同邂逅一位知己,邂逅一个完美之人。有时心生敬意,有时怦然心动。仿佛你心底埋藏多年的话,作者替你说了出来,你们在时光深处倾心相遇的一瞬间,情投意合,心旷神怡。