LUA's M-binary conversion to n-binary-arbitrary conversion algorithm

Source: Internet
Author: User

Boring enough to write this, to prevent the need for people also bored, write a bar

There are n kinds of algorithms, but we have enough.

1 --array in reverse order2 Local functionOrderbydesc (Input)3     LocalOutput = {}4     LocalCount = #input5      whileCount >0  Do6         Table.insert(output, Input[count])7Count = Count-1 8     End9     returnOutputTen End One  A --binary conversion, English not to use pinyin - --@dec 10 binary data, all right, just the numbers. - --@x, the most common of course is two, 86 or 16, binary the Local function _dec2X (Dec, x) -     --The calculation results are stored here -     LocalNew_number = {} -  +     --The algorithm is as follows: -         --9527 = 9* (10^3) +5* (10^2) +2* (10^1) +7* (10^0) +         --7 = 9527%10, 2 = (9527-7)%100/100 A         --f (n) = (dec% (X^i)-F (n-1))/x at         --F (0) = 0 -     --a parameter represents the first, returning whether to continue -     Local functionF (a) -         assert(A >=1) -         LocalMoD = Dec%Math.pow(x, a) -         LocalLast_mod = (A = =1) and 0 or assert(new_number[a-1]) inNew_number[a] = (mod-last_mod)/Math.pow(X, A-1) -         --take an integer part toNew_number[a] =MATH.MODF(New_number[a]) +         returnMoD ~=Dec -     End the     --The function obtains a bit value *     Locali =1 $      whileF (i) DoPanax Notoginsengi = i +1 -     End the      +     returnNew_number A End the  + --turn a data into an X -binary - --take 9527,10 as an example, {7, 2, 5, 9} $ Local function _numbertable2X (number_tbl,x) $     Localresult =0 -      forI,vinch Ipairs(NUMBER_TBL) Do -         Print(result,x, I, v) theresult = result + v*Math.pow(X, I-1) -     EndWuyi     returnresult the End -  Wu Local functiontest_dec2x () -     LocalKtestnumber =9527 About     LocalN1 =_dec2 x (Ktestnumber,Ten) $     --Table.foreach (N1, function (_,V) -     --Print (v) -     --end) -     assert(Ktestnumber = =_numbertable2 x (N1,Ten)) A End +TEST_DEC2X ()

LUA's M-binary conversion to n-binary-arbitrary conversion algorithm

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.