A simple 62-hexadecimal implementation is made.

Source: Internet
Author: User
The characters and order required for the 62 hexadecimal notation: 0... 9 a. z A. Z; only the 62 hexadecimal notation string and positive integer are exchanged.
Unit H62; interfaceuses sysutils; function inttoh62 (N: uint64): string; // convert the integer to the 62-digit string function h62toint (S: string): uint64; // convert the 62-digit string to the integer 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); // .. Z if charinset (C, ['A '.. 'Z']) Then exit (byte (c)-65 + 36); // .. 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'); // no more than 11 bits for I: = L downto 1 do begin C: = s [I]; N: = l-I; result: = Result + _ C2v (C, N); end.
// Test: 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-bit STR: = inttoh62 (I); // bui6zolztrh STR: = 'zzzzzzzzz'; // maximum I: = h62toint (STR); // 15143072536417990655; smaller than the maximum value (18446744073709551615) of uint64, and larger than the maximum value (9223372036854775807) of int64. end;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.