Corruption with modified Pstring library

I have been adding String like functionality to the PString library and have hot a snag.

I am getting some sort of corruption and bizarre behavior in serial monitor and I can’t see why.

I have this in my sketch…it should be obvious what I am trying to do.

But my toUint(…) function is returning zero and I have no idea why.

#include <PString.h>

void setup() 
{
/*
    const char* toStr(const bool bVal);
    const char* toStr(const uint32_t nNum, const uint8_t nBase);
    const char* toStr(const int32_t nNum, const uint8_t nBase);
    const char* toStr(const double dNum, const uint8_t nRealFormat);
  uint32_t toUint(const uint8_t nBase) const;
  int32_t toInt(const uint8_t nBase) const;
  double toReal(uint8_t nDecType) const;
*/
  Serial.begin(115200);

  char str[50];
  PString pstr(str, 50);
  pstr = "10";
  Serial.println((int)pstr.toUint(10));

/*
  pstr = "1010";
  Serial.println(pstr.toUint(2));
  pstr = "12";
  Serial.println(pstr.toUint(8));
  pstr = "A";
  Serial.println(pstr.toUint(16));
*/
}

void loop() 
{

}

Here is the function:

Debug printlns reveal that the index is advancing along the string as expected, my loop variables are being updated as expected, the case statement is being traversed as expected.

But nValue is inexplicably always zero with each loop iteration and I can’t see anything obviously wrong with my code.

And I am getting this in serial monitor:

@@@@0, ‚bŠb‚5
@@@@1, 10, 10, 0@@@@
0
@@@@0, 10, 1, 0@@@@
@@@@1, 10, 10, 0@@@@
0

I don’t know whether the bad chars are a result of serial issues or whether I have done something really bad in my class that is screwing up memory - nothing appears to be wrong as far as I can see.

Perhaps a set of fresh experience eyes will spot the problem.

uint32_t PString::toUint(const uint8_t nBase)
{
  int8_t nI = 0;
  uint32_t nVal = 0, nMult = 0;

  if ((nBase >= 2) && (nBase <= 16))
  {
	for (nI = strlen(m_strBuff) - 1, nMult = 1; nI >= 0; nI--, nMult *= nBase)
	{
	  switch (m_strBuff[nI])
	  {
		  case '0': nVal += nMult * (m_strBuff[nI] - '0'); break;
		  case '1': nVal += nMult * (m_strBuff[nI] - '1');break;
		  case '2': nVal += (m_strBuff[nI] - '2') * nMult;break;
		  case '3': nVal += (m_strBuff[nI] - '3') * nMult;break;
		  case '4': nVal += (m_strBuff[nI] - '4') * nMult;break;
		  case '5': nVal += (m_strBuff[nI] - '5') * nMult;break;
		  case '6': nVal += (m_strBuff[nI] - '6') * nMult;break;
		  case '7': nVal += (m_strBuff[nI] - '7') * nMult;break;
		  case '8': nVal += (m_strBuff[nI] - '8') * nMult;break;
		  case '9': nVal += (m_strBuff[nI] - '9') * nMult;break;
		  case 'A': nVal += (m_strBuff[nI] - 'A') * nMult;break;
		  case 'a': nVal += (m_strBuff[nI] - 'a') * nMult;break;
		  case 'B': nVal += (m_strBuff[nI] - 'B') * nMult;break;
		  case 'b': nVal += (m_strBuff[nI] - 'b') * nMult;break;
		  case 'C': nVal += (m_strBuff[nI] - 'C') * nMult;break;
		  case 'c': nVal += (m_strBuff[nI] - 'c') * nMult;break;
		  case 'D': nVal += (m_strBuff[nI] - 'D') * nMult;break;
		  case 'd': nVal += (m_strBuff[nI] - 'd') * nMult;break;
		  case 'E': nVal += (m_strBuff[nI] - 'E') * nMult;break;
		  case 'e': nVal += (m_strBuff[nI] - 'e') * nMult;break;
		  case 'F': nVal += (m_strBuff[nI] - 'F') * nMult;break;
		  case 'f': nVal += (m_strBuff[nI] - 'f') * nMult;break;
	  }
		Serial.print("@@@@");
		Serial.print(m_strBuff[nI]);
		Serial.print(", ");
		Serial.print(nBase);
		Serial.print(", ");
		Serial.print(nMult);
		Serial.print(", ");
		Serial.print(nVal);
		Serial.println("@@@@");

	}
  }
  return nVal;
}

PString.cpp (13.7 KB)

PString.h (5.37 KB)

Well anything multiplied by zero is zero...
And zero + zero is zero
So result is zero
:slight_smile:

If the hint is not big enough

case '2': nVal += (m_strBuff[nI] - '2') * nMult;break;

Let me know if you need clearer statement - you did get me confused at first too...

J-M-L:
If the hint is not big enough

case ‘2’: nVal += (m_strBuff[nI] - ‘2’) * nMult;break;

I knew it had to be something really dumb - shouldn’t have been trying to solve it at 2:00am I suppose.

Why the corrupted chars in the serial monitor? I take it that is why many example sketches have while (!Serial); in setup()?

:slight_smile:

I made your method into just a function in a small sketch to test it out and did not have any pb with the serial communication at 115200.

It is indeed not exceptional to have some garbage at the beginning of your connection after upload. I usually close and open again the serial console which relaunches your arduino program and then I get No bad chars