Delphi 62进制转10进制

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 C in ['0'..'9'] then Result:=Byte(C) - 48;      //0..9
  if C in ['a'..'z'] then Result:=Byte(C) - 97 + 10; //a..z
  if C in ['A'..'Z'] then Result:=Byte(C) - 65 + 36; //A..Z
end;

function _B2C(B: Byte): Char; inline;
begin
  Result := #0;
  if B <= 9 then Result:=Char(B + 48);                       //0..9
  if (B >= 10) and (B <= 35) then Result:=Char(B - 10 + 97); //a..z
  if (B >= 36) and (B <= 61) then Result:=Char(B - 36 + 65); //A..Z
end;

function _Power(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 Result:=_C2B(C);
  if (N > 0) then Result := _C2B(C) * _Power(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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值