Frage

I am trying to get the original 12-bit value from from a base15 (edit) string. I figured that I need a zerofill right shift operator like in Java to deal with the zero padding. How do I do this?

No luck so far with the following code:

static string chars = "0123456789ABCDEFGHIJKLMNOP";

static int FromStr(string s)
{
int n = (chars.IndexOf(s[0]) << 4) +
        (chars.IndexOf(s[1]) << 4) +
        (chars.IndexOf(s[2]));

return n;
}

Edit; I'll post the full code to complete the context

static string chars = "0123456789ABCDEFGHIJKLMNOP";

static void Main()
{
    int n = FromStr(ToStr(182));

    Console.WriteLine(n);
    Console.ReadLine();
}

static string ToStr(int n)
{
    if (n <= 4095)
    {
        char[] cx = new char[3];

        cx[0] = chars[n >> 8];
        cx[1] = chars[(n >> 4) & 25];
        cx[2] = chars[n & 25];

        return new string(cx);
    }

    return string.Empty;
}

static int FromStr(string s)
{
    int n = (chars.IndexOf(s[0]) << 8) +
            (chars.IndexOf(s[1]) << 4) +
            (chars.IndexOf(s[2]));

    return n;
}
War es hilfreich?

Lösung

Your representation is base26, so the answer that you are going to get from a three-character value is not going to be 12 bits: it's going to be in the range 0..17575, inclusive, which requires 15 bits.

Recall that shifting left by k bits is the same as multiplying by 2^k. Hence, your x << 4 operations are equivalent to multiplying by 16. Also recall that when you convert a base-X number, you need to multiply its digits by a power of X, so your code should be multiplying by 26, rather than shifting the number left, like this:

int n = (chars.IndexOf(s[0]) * 26*26) +
        (chars.IndexOf(s[1]) * 26) +
        (chars.IndexOf(s[2]));
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top