C #10-and 62-in conversion data with high pressure: 10-and-96-in conversion

Source: Internet
Author: User
Tags encode string

Because the project needs to convert the 10-digit ID into a 62-digit string and share it with others.

Therefore, I searched many algorithms on the Internet, but none of them met the requirements. The ID in the project is calculated by a fixed algorithm ~ The ulong integer of 20-bit data.

For example, 17223162272256398107,509488277152981097.

 

The algorithm found on the internet is that it is inaccurate to convert the ulong value to the ulong value after the ulong value is converted into a 62-digit string.

As a result, all kinds of testing and hypothetical problems are located, and finally the algorithm is determined to be correct, so we began to doubt the data type problem.

Sure enough, the reason is that Math is used. pow (double x, double y) (this method is used to calculate the y Power of x). The precision of double and float can only be precise to the last seven digits of the decimal number, however, decimals are not used in our algorithms, so the problem should be that the Pow method should be implemented internally and may be calculated in the form of power. Therefore, if the data is big, if it is expressed as a power that exceeds 7 bits, a calculation method similar to int will appear.

 

So I re-wrote the Pow method and solved the problem. The maximum value of the rewrite method can be up to the maximum value of decimal (79228162514264337593543950335), 28 BITs.

Theoretically, it can support infinite numeric values, but it only needs to rewrite the decimal type.

Go directly to the Code:

Public class Converter {private static String keys = "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; // encode the public class Converter {private static String keys =, reading is affected. Private static int exponent = keys. length; // power /// <summary> /// ulong value type to 62 string /// </summary> /// <param name = "value"> The max value can not more decimal. maxValue </param> // <returns> Return a specified 62 encode string </returns> public static string Decimal2Str (decimal value) // 17221_2558080896352ul {string result = string. empty; do {decimal index = value % exponent; result = keys [(int) index] + result; value = (value-index)/exponent ;} while (value> 0); return result ;} /// <summary> // 62 encode string to decimal // </summary> // <param name = "value"> 62 encode string </param>/ // <returns> Return a specified decimal number that decode by 62 string </returns> public static decimal Str2Decimal (string value) // bUI6zOLZTrj {decimal result = 0; for (int I = 0; I <value. length; I ++) {int x = value. length-I-1; result + = keys. indexOf (value [I]) * Pow (exponent, x); // Math. pow (exponent, x);} return result ;} /// <summary> /// the Npower of a data set /// </summary> /// <param name = "x"> </param> /// <returns> </returns> private static decimal Pow (decimal baseNo, decimal x) {decimal value = 1; // 1 will be the result for any number's power 0. 0 to the power of any number, the result is equal to 1 while (x> 0) {value = value * baseNo; x --;} return value ;}}

 

This friend's data type explanation is more detailed, see: http://www.cnblogs.com/Lxiaojiang/p/3631371.html

PS: Google's powerful blocking once again makes me hate a certain type of people and things.

Related Article

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.