1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespaceConsoleApplication17 {8 9 Public class NumberTen { One Public stringcharacters A { - Get; - Set; the } - - Public intLength - { + Get - { + if(Characters! =NULL) A returncharacters.length; at Else - return 0; - } - - } - in PublicNumber () - { toCharacters ="0123456789"; + } - the PublicNumber (stringcharacters) * { $Characters =characters;Panax Notoginseng } - the /// <summary> + ///converts a number to the specified binary form string A /// </summary> the /// <param name= "number" ></param> + /// <returns></returns> - Public stringToString (LongNumber ) $ { $list<string> result =Newlist<string>(); - Longt =Number ; - the while(T >0) - {Wuyi varMoD = t%Length; thet = Math.Abs (t/Length); - varcharacter =Characters[convert.toint32 (MoD)]. ToString (); WuResult. Insert (0, character); - } About $ return string. Join ("", result. ToArray ()); - } - - /// <summary> A ///specifies that the string is converted to a numeric form of the specified binary + /// </summary> the /// <param name= "str" ></param> - /// <returns></returns> $ Public LongFromString (stringstr) the { the Longresult =0; the intj =0; the foreach(varChinch New string(str. ToCharArray (). Reverse (). ToArray ())) - { in if(Characters.contains (CH)) the { theResult + = Characters.indexof (CH) * ((Long) Math.pow (Length, J)); AboutJ + +; the } the } the returnresult; + } - the }Bayi the the - class Program - { the Static voidPrint (LongNumber , number adapter) the { theConsole.WriteLine ("Enter Number: {0}", number); theConsole.WriteLine ("rule: {0}\t\t binary: {1} binary", adapter. Characters,adapter. Length); - varNumtostr =adapter. ToString (number); theConsole.WriteLine ("Conversion Result: {0}", numtostr); the varStrtonum =adapter. FromString (NUMTOSTR); theConsole.WriteLine ("Reverse Conversion result: {0}", strtonum);94 Console.WriteLine (); theConsole.WriteLine ("============ Boring split-line ============"); the Console.WriteLine (); the }98 About Static voidMain (string[] args) - {101 //Traditional 2- binary102Number N1 =NewNumber (" on");103 //Traditional 8- binary104Number N2 =NewNumber ("01234567"); the //Traditional 16- binary106Number N3 =NewNumber ("0123456789ABCDEF");107 //custom coded n-binary, this can be used to do verification code? 108Number N4 =NewNumber ("parents say the names are too long, hiding behind the trees and being discovered by the tribe.");109 //Cottage a short URL theNumber N5 =NewNumber ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");111 thePrint (65535, N1);113Print (65535, n2); thePrint (65535, N3); thePrint (65535, N4); thePrint (165535, N5);117 118 Console.readkey ();119 - }121 }122}
Transferred from: https://www.cnblogs.com/linxuanchen/archive/2012/02/02/2336099.html
C # Realization of arbitrary encoding of integer data word arbitrary binary conversion and inverse conversion