Skip to content

Instantly share code, notes, and snippets.

@richardtallent
Created July 20, 2016 03:56
Show Gist options
  • Save richardtallent/fa0bda39b057403d132c27798d34a170 to your computer and use it in GitHub Desktop.
Save richardtallent/fa0bda39b057403d132c27798d34a170 to your computer and use it in GitHub Desktop.
Integer Encoding in Base 16384
// Encodes a positive integer (or 0) in Base 2^14, using the Unicode CJK Unified Ideographs block as digits.
// This is a silly thing to do, but has a particular use case.
// Blog post here: http://blog.tallent.us/?p=368
public static int FromBase16384(this string value) {
if(string.IsNullOrEmpty(value)) throw new ArgumentException();
int letter;
int letterBase = 1;
int iout = 0;
for(var x = 0; x < value.Length; x++) {
letter = (int)value[x] - 0x4E00;
if(letter < 0 || letter > 0x3FFF) throw new ArgumentException();
iout += letter * letterBase;
letterBase = letterBase << 14;
}
return iout;
}
public static string ToBase16384(this int value) {
int letter;
string result = string.Empty;
if(value < 0) throw new ArgumentException("Negative numbers are not supported");
do {
letter = value % 0x4000;
result += (char)(0x4E00 + letter);
value = value >> 14;
} while(value > 0);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment