2

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;
}
badsyntax
  • 23
  • 3
  • What's the problem with the code you have? Can you give example inputs and expected outputs? – dtb Oct 13 '13 at 13:06
  • For example: 182 equals "09G" in base26, so that would be a valid input FromStr() should return 182, but it doesn't. – badsyntax Oct 13 '13 at 13:11
  • A few seconds ago there was a comment with your `ToStr` method. It seems (besides the problem in `FromStr` pointed out by @Dougles) there might also be some problems in `ToStr`. Please add the `ToStr` code to your question! – dtb Oct 13 '13 at 13:14
  • And there is your actual problem: `n & 25` is not the same as `n % 26`; `n >> 4` is not the same as `n / 26`. Your `ToStr` method is broken. – dtb Oct 13 '13 at 13:16
  • I think you misunderstand what I'm trying to do. I want to create a base15 string out of a value with a maximum length of 12-bits. – badsyntax Oct 13 '13 at 13:34

1 Answers1

0

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]));
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523