user40645: I have an algorithm that its main purpose is decoding, converting, or what have you a string of uppercase letters (A-Z) into a number. This is almost like converting a base 26 number into base 10, however I believe that a base 26 number encoded would actually include numbers, I am working strictly with uppercase letters, I digress.
I have an implantation working in C++11, in fact most of the controller algorithm was a direct copy, with a slight change to the indexOf method.
The C++11 version works as I expect it to. So initially I thought the new indexOf method was incorrect, but after a simple test, it is working as expected. I have narrowed down the problem to a line in the convertTo10 where I am calling the pow function.
As you can see from the Serial.println()'s in the convertTo10 method, the output is printing exactly what we expect, 0 & 26. 
However, the output of this function is 25. WHAT? 
I have read the docs on the pow method from the Arduino lib, and it takes in two floats and returns a double. Even if the issue was a type conversion issue, converting a double to an int truncates the number, it does not round it. Well, this is from working with C++.
Any help with this issue would be much appreciated.
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <inttypes.h>

#include <Arduino.h>
#include <avr/pgmspace.h>
#include <Wire.h>


const char _values[26] = {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
    'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
    'U', 'V', 'W', 'X', 'Y', 'Z'
};

const int _val_len = 26;


int indexOf(char needle);
int convertTo10(String value, int len);

String x = "BA";
int len = 2;

void loop() {  
  Serial.print(x);
  Serial.print("=> ");
  Serial.println(convertTo10(x, 2));
  delay(50000);
}

int convertTo10(String value, int len) {
    int lastIndex = len - 1;
    double output = 0;
    int pos = 0;
    Serial.println();
    for (int i = lastIndex; i >= 0; i--) {
        int r = indexOf(value[i]);

        output += (r * pow((_val_len, pos));
        Serial.print("     output => ");
        Serial.println( output );
        pos++;
    }
    return output;
}

int indexOf(char needle) {

    int first = 0; 
    int last = _val_len - 1; 
    int mid;  

  do {   
    mid = (first + last) / 2; 

    if (_values[mid] == needle)
      return mid;
    if (_values[mid] > needle)
      last = mid - 1;
    else
      first = mid + 1; 
  } while (first <= last);

  return -1; 
}

For Reference, Here what it is outputting:
BA =>
     output => 0.00
     output => 26.00 
25


user30005: way too complicated:
int convertTo10(char *num_26, int len) {
  int tmp = 0;  //hold the output
  int i = 0;  //indexing the input
  while (len--) tmp = tmp * 26 + (num_26[i++]-'A');
  return tmp;
}

note:

"* 26" can be further simplified;

error checking could be built-in here, or somewhere else (better).

better structuring could get rid of one of the temporary variables.



user10794: Your code is incredibly complex for a simple thing like converting a number from another base. This works (prints 26):
const char * test = "BA";

long convertTo10 (const char * s)
  {
  long result = 0;
  for (const char * p = s; *p; p++)
    {
    result *= 26;   // base
    result += *p - 'A';  // make zero-relative
    }
  return result;
  } // end of convertTo10

void setup() 
  {
  Serial.begin (115200);
  Serial.println ("Starting.");
  Serial.println (convertTo10 (test));
  }


void loop() { }

With input "CBA" it prints 1378 as expected.  Sketch size: 2,300 bytes of program memory. The sketch size reduces to 2,236 bytes if I make the function return int (as you did) rather than long.
You don't need to use String (in fact it saves RAM and avoids memory fragmentation to not use it). You don't need to pass the length, with both char* and String you can deduce the length. If you use long as the return type you can handle larger numbers.
By using pow you are bringing in a library which is not needed and which will consume program memory. By using double you are introducing possible inaccuracies since double does not necessarily store numbers exactly. Your code used 7,604 bytes of program memory once I corrected the syntax error and added an empty setup function.

Also, the method I illustrate above is much faster. I timed it at converting "BA" in 4.3 µs, whereas the method using the pow function takes 442 µs. So the simpler method is also 100 times faster.

40645: <p>I have an algorithm that its main purpose is decoding, converting, or what have you a string of uppercase letters (A-Z) into a number. This is almost like converting a base 26 number into base 10, however I believe that a base 26 number encoded would actually include numbers, I am working strictly with uppercase letters, I digress.</p>

<p>I have an implantation working in C++11, in fact most of the controller algorithm was a direct copy, with a slight change to the indexOf method.</p>

<p>The C++11 version works as I expect it to. So initially I thought the new indexOf method was incorrect, but after a simple test, it is working as expected. I have narrowed down the problem to a line in the convertTo10 where I am calling the pow function.</p>

<p>As you can see from the Serial.println()'s in the convertTo10 method, the output is printing exactly what we expect, 0 &amp; 26. </p>

<p>However, the output of this function is 25. WHAT? </p>

<p>I have read the docs on the pow method from the Arduino lib, and it takes in two floats and returns a double. Even if the issue was a type conversion issue, converting a double to an int truncates the number, it does not round it. Well, this is from working with C++.</p>

<p>Any help with this issue would be much appreciated.</p>

<pre><code>#include &lt;string.h&gt;
#include &lt;stdio.h&gt;
#include &lt;errno.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;inttypes.h&gt;

#include &lt;Arduino.h&gt;
#include &lt;avr/pgmspace.h&gt;
#include &lt;Wire.h&gt;


const char _values[26] = {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
    'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
    'U', 'V', 'W', 'X', 'Y', 'Z'
};

const int _val_len = 26;


int indexOf(char needle);
int convertTo10(String value, int len);

String x = "BA";
int len = 2;

void loop() {  
  Serial.print(x);
  Serial.print("=&gt; ");
  Serial.println(convertTo10(x, 2));
  delay(50000);
}

int convertTo10(String value, int len) {
    int lastIndex = len - 1;
    double output = 0;
    int pos = 0;
    Serial.println();
    for (int i = lastIndex; i &gt;= 0; i--) {
        int r = indexOf(value[i]);

        output += (r * pow((_val_len, pos));
        Serial.print("     output =&gt; ");
        Serial.println( output );
        pos++;
    }
    return output;
}

int indexOf(char needle) {

    int first = 0; 
    int last = _val_len - 1; 
    int mid;  

  do {   
    mid = (first + last) / 2; 

    if (_values[mid] == needle)
      return mid;
    if (_values[mid] &gt; needle)
      last = mid - 1;
    else
      first = mid + 1; 
  } while (first &lt;= last);

  return -1; 
}
</code></pre>

<p>For Reference, Here what it is outputting:</p>

<pre><code>BA =&gt;
     output =&gt; 0.00
     output =&gt; 26.00 
25
</code></pre>
 30005: <p>way too complicated:</p>
<pre><code>int convertTo10(char *num_26, int len) {
  int tmp = 0;  //hold the output
  int i = 0;  //indexing the input
  while (len--) tmp = tmp * 26 + (num_26[i++]-'A');
  return tmp;
}
</code></pre>
<p>note:</p>
<ol>
<li><p>&quot;* 26&quot; can be further simplified;</p>
</li>
<li><p>error checking could be built-in here, or somewhere else (better).</p>
</li>
<li><p>better structuring could get rid of one of the temporary variables.</p>
</li>
</ol>
 10794: <p>Your code is incredibly complex for a simple thing like converting a number from another base. This works (prints 26):</p>

<pre><code>const char * test = "BA";

long convertTo10 (const char * s)
  {
  long result = 0;
  for (const char * p = s; *p; p++)
    {
    result *= 26;   // base
    result += *p - 'A';  // make zero-relative
    }
  return result;
  } // end of convertTo10

void setup() 
  {
  Serial.begin (115200);
  Serial.println ("Starting.");
  Serial.println (convertTo10 (test));
  }


void loop() { }
</code></pre>

<p>With input "CBA" it prints 1378 as expected.  Sketch size: 2,300 bytes of program memory. The sketch size reduces to 2,236 bytes if I make the function return <code>int</code> (as you did) rather than <code>long</code>.</p>

<p>You don't need to use <code>String</code> (in fact it saves RAM and avoids memory fragmentation to not use it). You don't need to pass the length, with both <code>char*</code> and <code>String</code> you can deduce the length. If you use <code>long</code> as the return type you can handle larger numbers.</p>

<p>By using <code>pow</code> you are bringing in a library which is not needed and which will consume program memory. By using <code>double</code> you are introducing possible inaccuracies since double does not necessarily store numbers exactly. Your code used 7,604 bytes of program memory once I corrected the syntax error and added an empty <code>setup</code> function.</p>

<hr>

<p>Also, the method I illustrate above is <em>much</em> faster. I timed it at converting "BA" in 4.3 µs, whereas the method using the <code>pow</code> function takes 442 µs. So the simpler method is also 100 times faster.</p>
