Converting from strings to byte arrays with LINQ in C#
Edit: corrected an issue with leading zero bytes in the result.

I'll focus on the GetOctalBytes method in this review.

Also, for the explanation I assume that the string is processed right-to-left (reverse order) and similarily, the resulting byte array starts with the lowest byte at index 0. Those assumptions will be represented / corrected in the final code.

For octal strings, a group of 8 characters forms a group of up to 3 result bytes. So for each complete group, take 3 bytes. The following table shows important properties for different character counts of a group.

(in-group character count) (bits count) (bytes needed) (affected byte indices in group)
1                           3           1              0
2                           6           1              0
3                           9           2              0, 1
4                          12           2                 1
5                          15           2                 1
6                          18           3                 1, 2
7                          21           3                    2
8                          24           3                    2


The total bytes needed can be calculated as (octal.Length * 3 + 7) / 8. However, for length 3 or 6, the actually needed byte count can be lower, if the character value needs only 2 or 1 of its bits. So for these cases, the number of bytes can be lowered. Since a new group starts every 8 characters and each group needs 3 bytes, the starting index of the current byte group in the result array can be calculated as (character_index / 8) * 3. The affected in-group indices are calculated from the remainder:

remainder = (character_index % 8);
if (remainder &lt; 3) group-index 0 affected
if (remainder &gt;= 2 &amp;&amp; remainder &lt; 6) group-index 1 affected
if (remainder &gt;= 5) group-index 2 affected


In order to avoid leading zero bytes, the character value has to be considered additionally for the second and third byte.

I hope these explanations are enough introduction for the following code suggestion:

Note: I ditched the linq solution in favor of a more readable solution with less array-recreation.

public static byte[] GetOctalBytes(this string octal, bool preTrimmed = false)
{
    if (!preTrimmed)
    {
        octal = octal.Trim();
        if (octal.StartsWith("0o", StringComparison.OrdinalIgnoreCase) || octal.StartsWith("8#"))
            octal = octal.Substring(2);
    }

    octal = octal.TrimStart('0');
    if (octal.Length == 0)
        return new byte[] { 0 };

    var arrayLength = (octal.Length * 3 + 7) / 8;
    var inGroup = (octal.Length % 8);
    if ((inGroup == 3 &amp;&amp; octal[0] &lt; '4') ||
        (inGroup == 6 &amp;&amp; octal[0] &lt; '2'))
    {
        --arrayLength;
    }

    var result = new byte[arrayLength];

    for (int i = 0; i &lt; octal.Length; i++)
    {
        var baseIndex = (i / 8) * 3;
        var shift = (i % 8) * 3;
        var valueInGroup = (octal[octal.Length - i - 1] - '0') &lt;&lt; shift;

        result[result.Length - baseIndex - 1] |= (byte)(valueInGroup &amp; 0xff);
        if (valueInGroup &gt; 0xff)
            result[result.Length - baseIndex - 2] |= (byte)((valueInGroup &gt;&gt; 8) &amp; 0xff);
        if (valueInGroup &gt; 0xffff)
            result[result.Length - baseIndex - 3] |= (byte)((valueInGroup &gt;&gt; 16) &amp; 0xff);
    }

    return result;
}