It was a very boring afternoon. I was bored with writing such a thing that could convert and reverse convert arbitrary integers according to arbitrary encoding and any hexadecimal conversion.
Purpose:
Evil .. I am not sure about this. At present, it seems that it can be used for verification code, for short URLs, and others? Not yet.
First:
The implementation is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace ConsoleApplication1
{
Public class Number
{
Public string Characters
{
Get;
Set;
}
Public int Length
{
Get
{
If (Characters! = Null)
Return Characters. Length;
Else
Return 0;
}
}
Public Number ()
{
Characters = "0123456789 ";
}
Public Number (string characters)
{
Characters = characters;
}
/// <Summary>
/// Convert a number to a specified hexadecimal string
/// </Summary>
/// <Param name = "number"> </param>
/// <Returns> </returns>
Public string ToString (long number)
{
List <string> result = new List <string> ();
Long t = number;
While (t> 0)
{
Var mod = t % Length;
T = Math. Abs (t/Length );
Var character = Characters [Convert. ToInt32 (mod)]. ToString ();
Result. Insert (0, character );
}
Return string. Join ("", result. ToArray ());
}
/// <Summary>
/// Convert a specified string to a specified numeric format
/// </Summary>
/// <Param name = "str"> </param>
/// <Returns> </returns>
Public long FromString (string str)
{
Long result = 0;
Int j = 0;
Foreach (var ch in new string (str. ToCharArray (). Reverse (). ToArray ()))
{
If (Characters. Contains (ch ))
{
Result + = Characters. IndexOf (ch) * (long) Math. Pow (Length, j ));
J ++;
}
}
Return result;
}
}
Class Program
{
Static void Print (long number, Number adapter)
{
Console. WriteLine ("input number: {0}", number );
Console. WriteLine ("rule: {0} \ t hexadecimal: {1} hexadecimal", adapter. Characters, adapter. Length );
Var numtostr = adapter. ToString (number );
Console. WriteLine ("Conversion Result: {0}", numtostr );
Var strtonum = adapter. FromString (numtostr );
Console. WriteLine ("reverse Conversion Result: {0}", strtonum );
Console. WriteLine ();
Console. WriteLine ("=============== boring split line =============== ");
Console. WriteLine ();
}
Static void Main (string [] args)
{
// The traditional BINARY SYSTEM
Number n1 = new Number ("01 ");
// Traditional octal
Number n2 = new Number ("01234567 ");
// Traditional hexadecimal
Number n3 = new Number ("0123456789 ABCDEF ");
// Custom encoding in the N-base format. Can this be used for verification code?
Number n4 = new Number ("My aunt said the name is too long to be found by the tribe after the tree ");
// Shanzhai a short website
Number n5 = new Number ("0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
Print (65535, n1 );
Print (65535, n2 );
Print (65535, n3 );
Print (65535, n4 );
Print (165535, n5 );
Console. ReadKey ();
}
}
}
Finished, just.
From Lin's column