OBOE: Need help understanding, possible type conversion issue 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 &amp; 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 &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; 
}


For Reference, Here what it is outputting:

BA =&gt;
     output =&gt; 0.00
     output =&gt; 26.00 
25
way too complicated:

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


note:
 1. "* 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.