C # implement conversion and inverse conversion of arbitrary encoding of integer data words

Source: Internet
Author: User

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

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.