1

I stumbled on this method which should do a base convert from a 10 base to radix base, so for example if I pass 28, 16 it should return 1c which is the hexadecimal representation of the decimal 28

private static string convertTo(long value, int radix)
{
    char[] alphabet = "0123456789abcdefghijklmnopqrstuv".ToCharArray();
    string result = "";

    if (value < radix) {
        return alphabet[value].ToString();
    }

    long index;
    while (value != 0)
    {
        index = value % radix;
        value = Convert.ToInt64(Math.Floor(value / radix));  
        result += alphabet[index].ToString();
    }
    return result;
}

I'm rewriting that part of the program in PHP. By reading the code above, and predicting output manually, it returns c1 where it should return 1c for 28, 16

My finding is that this method returns a reversed representation of string in a given base, c1 instead of correctly 1c

Because I do not have a C# compiler I could not verify my findings.

So here are my two questions:

  1. Are my calculations right that the above method calld with 28, 16 returns c1?
  2. I suppose symbols (digits/alphabets), in any base, are written so that base exponents decreases by 1 as we go from left to write, so for example in decimal representation 312 means 300 (3 * 10 ^ 2) + 10 (1 * 10 ^ 1) + 2 (2 * 10 ^ 0).. is that absolutely always correct?
doc_id
  • 1,363
  • 13
  • 41
  • @GeraldSchneider correct, and that is a flipped c1 (which I believe is the return of that method above). If that's all correct I would end up using strrev( base_convert( 28, 10, 16 ) ) to achieve that effect. but I'm not sure of my findings and that's the question :) – doc_id Jan 03 '16 at 07:19
  • There are multiple free C# compilers available including Visual Studio Community. What's stopping you from testing it? – Jeff Jan 03 '16 at 07:42
  • @Jeff I do not have Windows and I cannot use VS. And without VS I will have to wrap this function in a class to test it, but I do not have that skill with C#. – doc_id Jan 03 '16 at 07:44
  • 2
    You can compile and test C# code here: http://csharppad.com/ – Gerald Schneider Jan 03 '16 at 07:45
  • You can also use mono under Linux. – Jeff Jan 03 '16 at 07:46
  • Side note: if you happen to hit this question by searching for base/radix conversion - correct answer http://stackoverflow.com/questions/923771/quickest-way-to-convert-a-base-10-number-to-any-base-in-net – Alexei Levenkov Jan 03 '16 at 07:53
  • I was also thinking if MS is doing something differently, in a reversed model, who knows what else... and which is more wise? asking an expert or testing a code for a language I never used? – doc_id Jan 03 '16 at 07:57

3 Answers3

2
  1. For me, yes. the snippet in C# (as it is) should return C1, instead of 1C. You will need to reverse the string result before returning it. (or use instead something like result = string.Concat(alphabet[index].toString()); when creating the result string)

  2. That's correct. It also works in all the other bases, i.e. if we take your example (28), you'll have :

28 = 2 * 10^1 + 8 * 10^0 (base 10)

28 = 1 * 16^1 + 12 * 16^0 (base 16) = 1C

28 = 3 * 8^1 + 2 * 8^0 (base 8) = 32

etc, etc.

Paul Picard
  • 150
  • 9
1

Yes, that code needs to reverse the output. Below are the results when I ran that code in Visual Studio 2015, and the corresponding output in the Locals window.

    var ret1 = convertTo(28, 16);
    var ret2 = convertTo(28, 10);
    var ret3 = convertTo(10, 10);

    ret1    "c1"    string
    ret2    "82"    string
    ret3    "01"    string
Kory Gill
  • 6,993
  • 1
  • 25
  • 33
1

Yes you are correct convertTo(28, 16) == c1 The loop in the code should be:

while (value != 0)
{
    index = value / radix; // implicit integer division  
    value = value % radix;
    result += alphabet[index].ToString();
}

As to your second question I believe that to be correct also - that's more for a mathematics expert to verify though.

ipinak
  • 5,739
  • 3
  • 23
  • 41
norlesh
  • 1,749
  • 11
  • 23