Created
March 7, 2012 02:31
-
-
Save wangye/1990509 to your computer and use it in GitHub Desktop.
Base24 encode and decode C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Description: Base24 encode and decode | |
// Author: wangye <pcn88 at hotmail dot com> | |
// Website: http://wangye.org | |
// | |
public static class Base24 | |
{ | |
private static string sel = "BCDFGHJKMPQRTVWXY2346789"; | |
public static string Encode(string Text) | |
{ | |
int i = 0; | |
int Pos = 0; | |
char []Buf = new char[Text.Length<<1]; | |
while ((i = Pos) < Text.Length) | |
{ | |
Buf[i<<1] = sel[(Text[Pos]) >> 4]; | |
Buf[(i<<1) + 1] = sel[23 - (Text[Pos] & 0x0F)]; | |
Pos++; | |
} | |
return new string(Buf); | |
} | |
public static string Decode(string Text) | |
{ | |
if (Text.Length % 2 != 0) | |
return null; | |
int [] NPos = new int[2]; | |
char[] N = new char[2]; | |
char[] Buf = new char[Text.Length >> 1]; | |
for (int i = 0; i < (Text.Length >> 1); i++) | |
{ | |
NPos[0] = sel.IndexOf(Text[ i<<1 ]); | |
NPos[1] = 23 - sel.IndexOf(Text[(i<<1) + 1]); | |
if (NPos[0] < 0 || NPos[1] < 0) { | |
return null; | |
} | |
Buf[i] = ((char)((NPos[0] << 4) | NPos[1])); | |
} | |
return new string(Buf); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment