public static byte[] Pack(string text)
{
char[] chars = text.ToCharArray();
byte[] best = null;
List<byte> pack = new List<byte>();
for (byte p = 0; p < 8; p++)
{
unchecked
{
pack.Clear();
bool hi = true;
byte b = p;
for (int c = 0; c < chars.Length; c++)
{
byte i = 0;
for (i = 0; i < 0xf; i++)
{
if (PATTERNS[p, i] == chars[c])
{
break;
}
}
if (i == 0xf)
{
byte ascii = (byte)chars[c];
if (hi)
{
b |= (byte)(i << 4);
pack.Add(b);
pack.Add(ascii);
hi = false;
}
else
{
b = i;
b |= (byte)(ascii & 0xf0);
pack.Add(b);
b = (byte)(ascii & 0xf);
hi = true;
}
}
else if (hi)
{
b |= (byte)(i << 4);
pack.Add(b);
hi = false;
}
else
{
b = i;
hi = true;
}
}
if (hi)
{
b |= 0xf0;
pack.Add(b);
}
}
if (best == null || pack.Count < best.Length)
{
best = pack.ToArray();
}
}
byte[] normal = ASCIIEncoding.ASCII.GetBytes(text);
if (normal.Length + 1 <= best.Length)
{
best = new byte[normal.Length + 1];
best[0] = 0xff;
Array.Copy(normal, 0, best, 1, normal.Length);
}
return best;
}