做了一个 62 进制的简单实现    


62 进制需要的字符及次序: 0..9 a..z A..Z; 只实现了 62 进制字符串与正整数的互换.



unit H62;interfaceuses SysUtils;function IntToH62(N: UInt64): string; //整数转到 62 进制字符串function H62ToInt(S: string): UInt64; //62 进制字符串转到整数implementationfunction _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..Zend;function _B2C(B: Byte): Char; inline;begin  Result := #0;  if B = 10) and (B = 36) and (B  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.


//测试: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;


©著作权归作者所有:来自51CTO博客作者JLee79的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 测试字符串写入类: TStringWriter
  2. 测试字符串读取类: TStringReader
  3. 将字符串转换成 UTF8 编码的函数
  4. 学用 ASP.Net 之 System.TimeSpan 结构
  5. 学用 ASP.Net 之 "字符串" (1): 基础
  6. 学用 ASP.Net 之 "字符串" (6): StringInfo 类
  7. 学用 ASP.Net 之 "字符串" (5): StringBuilder 类
  8. 学用 ASP.Net 之 "字符串" (4): string 类的扩展方法
  9. 学 Win32 汇编[19]: 查看二进制等相关函数

随机推荐

  1. 什么是Android?
  2. Android结构介绍
  3. 2012-7-17 AndroidManifest.xml中android
  4. android输入限制
  5. 自定义View系列教程01--常用工具介绍
  6. Android(安卓)滚动的公告栏
  7. Android用户界面 UI组件--AdapterView及
  8. android 牛人必修 ant 编译android工程
  9. android学习笔记
  10. 【转】Android xml资源文件中@、@android