81 binary to shorten a long integer in a multi-input way

Source: Internet
Author: User

Recently in the project some resources to use the unique identifier code, this code to be globally unique, so long, there are 25 bits, long can only be processed to 19 bits. Another 25-bit long integer reading display is not ideal, so a multi-input conversion class has been developed. Thought approaches to replace a domain name with a few characters. The following is the implementation of the specific Java code, currently supported to 81 binary.

1 ImportJava.math.BigInteger;2 Importjava.util.Arrays;3 Importjava.util.Date;4 5 /**6  * 7  * @authorProgrammer Lao Liu @2015-07-288 * @Description Convert decimal numbers to 36, 62, or 81 to make the length of the expression shorter, for example, 99999 turns into 62 q0t9  */Ten  Public classmultinumberation { One  A     Private Final Static Char[] flag = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', -' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', -' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', the' J ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ', -'! ', ' # ', '% ', ' & ', ' $ ', ' * ', ' (', ') ', ' [', '] ', ' {', '} ', ' ^ ', ' ~ ', '? ', ' @ ', ' '? ', -' < ', ' = ' }; -  +      Public Static intGetdeep () { -         returnflag.length; +     } A  at      Public Static intFindinflag (String f) { -         Charc = (Char) f.getbytes () [0]; -          for(inti = 0; i < flag.length; i++) { -             if(Flag[i] = =c) -                 returni; -         } in         return-1; -     } to  +      Public StaticBigInteger ToDecimal (String Multi,intDeep ) { -BigInteger result =NewBigInteger ("0"); the         if(Deep < 1 | | deep > Flag.length | |-deep = 10) *             returnresult; $ Panax NotoginsengBigInteger d =biginteger.valueof (deep); -          for(inti = 0; I < multi.length (); i++) { the             intpos = Findinflag (multi.substring (i, i + 1)); +             //arrays.binarysearch (flag, (char) (multi.substring (i, i + A             //1). GetBytes () [0]); the  +result = Result.add (D.pow (Multi.length ()-i-1). Multiply (biginteger.valueof (POS) )); -         } $         returnresult; $     } -  -     /** the      *  - * @Title:Wuyi * @Description: Converts decimal integers to the specified number of binary the      * @paramdecimal - *--decimal integer Wu      * @param Deep - *--Select the system, from 2~81 About      * @return $      */ -      Public StaticString Tomulti (BigInteger decimal,intDeep ) { -         if(Deep < 1 | | deep > Flag.length | |-deep = 10) -             return""; A         //take the remainder +BigInteger d =biginteger.valueof (deep); thebiginteger[] Bigdivide =Decimal.divideandremainder (d); -         intremainder = bigdivide[1].intvalue (); $String result = "" + flag[(int) remainder]; the  the         //Pick up Business theBigInteger quotient = bigdivide[0]; the         //quotient Rain The specified number of incoming numbers continues -         if(Quotient.compareto (d) >= 0) { inresult = Tomulti (quotient, deep) +result; the}Else { theresult = "" + flag[(int) Quotient.intvalue ()] +result; About         } the  the         returnresult; the     } +  -      Public Static voidMain (string[] args) { the         intDeep =multinumberation.getdeep ();Bayi         //System.out.println ("deep=" + deep); the         Longs =NewDate (). GetTime (); the         //Multinumeration.tomulti (238328L, deep); -          for(Longi = 1000000; i < 1000002; i++) { -String Code =string.valueof (i); the             //String code = the             //Createcodehelper.createcode (seqobjectname.servicegroup_category); the             //System.out.println ("code=" + code); theBigInteger big =NewBigInteger (code); -  theString ret =Multinumberation.tomulti (big, deep); theSYSTEM.OUT.PRINTLN (code + ">>" + ret + "===>" +Multinumberation.todecimal (ret, deep)); the         }94System.out.println ("deep=" + deep + ", timeout:" + (NewDate (). GetTime ()-s)); the     } the}

81 binary to shorten a long integer in a multi-input way

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.