Snippets

Craig Phillips C# implementation of the Bubble Babble algorithm

Created by Craig Phillips
public static class BubbleBabble
{
    private static readonly string vowels = "aeiouy";
    private static readonly string consonants = "bcdfghklmnprstvzx";
    public static string Convert(byte[] bytes)
    {
        int seed = 1;
        int rounds = 1 + bytes.Length / 2;
 
        StringBuilder result = new StringBuilder();
 
        result.Append(consonants[16]);
 
        for (int i = 0; i < rounds; i++)
        {
            if (i + 1 < rounds || bytes.Length % 2 == 1)
            {
                result.Append(vowels[(((bytes[2 * i] >> 6) & 3) + seed) % 6]);
                result.Append(consonants[(bytes[2 * i] >> 2) & 15]);
                result.Append(vowels[((bytes[2 * i] & 3) + seed / 6) % 6]);
 
                if (i + 1 < rounds)
                {
                    result.Append(consonants[(bytes[2 * i + 1] >> 4) & 15]);
                    result.Append("-");
                    result.Append(consonants[bytes[2 * i + 1] & 15]);
                    seed = (seed * 5 + bytes[2 * i] * 7 + bytes[2 * i + 1]) % 36;
                }
            }
            else
            {
                result.Append(vowels[seed % 6]);
                result.Append(consonants[16]);
                result.Append(vowels[seed / 6]);
            }
        }
 
        result.Append(consonants[16]);
 
        return result.ToString();
    }
}

Comments (0)