Using a linxuanchen document 《C # implement conversion and inverse conversion of arbitrary encoding of integer data wordsIs used to compress the hexadecimal string with a 62-digit string. The conversion process is mainly based on the original author. The overall idea is as follows: 1.16 hexadecimal string (0-9A-F) is converted to an Int64 integer, for example: (hex16) DAFDE = (hex10) 8967342. convert Int64 Integers to 62 hexadecimal numbers (0 ~ 9a-zA-Z): 3LlA. (The linxuanchen code has been implemented) several issues that need attention: 1. the maximum Int64 value is Int64.MaxValue, and the constant value is 9,223,372,036,854,775,807; that is, the hexadecimal 0x7fffffffffffff. Therefore, when compressing strings, you need to process the hexadecimal String Length. At most 15 characters can be compressed at a time to avoid overflow. 2. The output 62-digit string must be separated by a delimiter; otherwise, the string cannot be decoded. The direct code is as follows:
Using System; using System. collections. generic; // using System. linq; using System. text; // @ http://www.cnblogs.com/linxuanchen/archive/2012/02/02/2336099.html// @ shadu {AT} foxmail. comnamespace HexCover {public class MyString {// <summary> // use MaxLength to separate strHex16 strings /// </summary> /// <param name = "strHex16"> </param> /// <param name = "MaxLength"> </param> /// <returns> </returns> public static List <String> SplitHex16StingWithLength (string strHex16, int MaxLength) {string tmpStr = ""; List <string> StringList = new List <string> (); int count = strHex16.Length/MaxLength; if (strHex16.Length % MaxLength! = 0) {for (int I = 0; I <= count; I ++) {if (strHex16.Length-I * MaxLength)> MaxLength) {tmpStr = strHex16.Substring (I * MaxLength, MaxLength); StringList. add (tmpStr);} else {tmpStr = strHex16.Substring (I * MaxLength, (strHex16.Length % MaxLength); StringList. add (tmpStr) ;}}else {for (int I = 0; I <count; I ++) {tmpStr = strHex16.Substring (I * MaxLength, MaxLength); StringList. add (tm PStr) ;}} return StringList;} 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 the number to the 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 % Lengt H; t = Math. abs (t/Length); var character = Characters [Convert. toInt32 (mod)]. toString (); result. insert (0, character);} return string. join ("", result. toArray ());} /// <summary> /// convert the specified string to a number in the specified hexadecimal format. /// </summary> /// <param name = "str"> </param >/// <returns> </returns> public long FromString (string str) {long result = 0; int j = 0; char [] arr = str. toCharArray (); Array. reverse (arr); // return new String (arr); foreach (char ch in new string (arr) // foreach (var ch in new string (str. toCharArray (). reverse (). toArray () {// Characters. contains (if (Characters. contains (ch. toString () {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. writeL Ine ("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) {// traditional binary Number n1 = new Number ("0 1 "); // The traditional octal Number n2 = new Number (" 01234567 "); // The traditional hexadecimal Number n3 = new Number ("0123456789 ABCDEF"); // custom encoding in the N-hexadecimal format. Can this be used for verification code? Number n4 = new Number ("My aunt said that the name is too long and will be found by the tribe behind the tree"); // The name of a short website in shanzhai: Number n5 = new Number ("0123456789 abcdefghijklmnopqrstuvwxyz "); print (65535, n1); Print (65535, n2); Print (65535, n3); Print (65535, n4); Print (72057594037927934, n5); Console. readKey (); // ==== Added by Laien @ 2012-09-25 string strHex = "fffffffffffff31_cdafde"; string tmpStr = ""; List <string> strList = MyString. splitHex16StingWithLength (strHex, 15); foreach (string s in strList) tmpStr + = "-" + n5.ToString (Convert. toInt64 (s, 16); Console. writeLine ("hexadecimal string before conversion: {0}", strHex); Console. writeLine ("62 string after conversion: {0} (-separator)", tmpStr); Console. readKey ();}}}
The running result is as follows: