Adding some leading characters to a string

I've got a number (in this case between 0 and 1023, but it could be longer. I'm trying to output it to a display with '_NumOfDigits' digits so I need to add leading zeros. eg if the number is 123 and I have 7 digints then I want '0000123' as my string.

I've had a stab at it using strlen but it hasn't worked. Something to do with chars and strings. This is what I tried but I'm happy to go another way:

Snippet

  char strDisplay;
  int x;
  int NumberSize;
  int AddZeros;
  int res;
  int shiftword;
  
  strDisplay = String(intNumber, DEC);
  NumberSize = strlen(strDisplay);
  AddZeros = _NumOfDigits - NumberSize;
  
  if ( AddZeros > 0)
  {
    for (x =0; x <= AddZeros; x++)
  {
  strDisplay =  "0" + strDisplay;
  }
  }

In context:

#include "Arduino.h"
#include "Shifter.h"

Shifter::Shifter(int NumOfDigits, int SRCKPin, int SERINPin, int RCKPin)
{
  _NumOfDigits = NumOfDigits;
  pinMode(SRCKPin, OUTPUT);
  _SRCKPin = SRCKPin;
  pinMode(SERINPin, OUTPUT);
  _SERINPin = SERINPin;
  pinMode(RCKPin, OUTPUT);
  _RCKPin = RCKPin;    
}

void Shifter::clear()
{
  int x; 
  digitalWrite(_RCKPin, LOW);  
  // shift out 0's to clear display
  for (x = _NumOfDigits; x >= 0; x--)
    {
      shiftOut(_SERINPin, _SRCKPin, LSBFIRST, 0); 
    }
  digitalWrite(_RCKPin, HIGH);
}

int Shifter::display(int intNumber)
{ // Displays number on display
  int SegmentArray[] = {252, 96, 218, 242, 102, 182, 190, 224, 254, 230};
  int SegmentArrayDP[] = {253, 97, 219, 243, 103, 183, 191, 225, 255, 231};
  char strDisplay;
  int x;
  int NumberSize;
  int AddZeros;
  int res;
  int shiftword;
  
  strDisplay = String(intNumber, DEC);
  NumberSize = strlen(strDisplay);
  AddZeros = _NumOfDigits - NumberSize;
  
  if ( AddZeros > 0)
  {
    for (x =0; x <= AddZeros; x++)
  {
  strDisplay =  "0" + strDisplay;
  }
  }
  
  
  digitalWrite(_RCKPin, LOW);
  
  for (x = _NumOfDigits ; x >= 0; x--)
  {
  res = strDisplay[x] - '0';
  if (x == 2) 
  { 
  shiftword = SegmentArrayDP[res];
  }
  else 
  { 
  shiftword = SegmentArray[res];
  }
  // shift out the bits
  shiftOut(_SERINPin, _SRCKPin, LSBFIRST, shiftword);
  }
  digitalWrite(_RCKPin, HIGH);
}

Simplest method - use sprintf.

int myNumber = 123;
char buffer[8];

sprintf(buffer,"%07d",myNumber);
// buffer now contains "0000123\0"

Thanks majenko that seems to have worked. Just one more question (for now :)). How do I make the "%07d" based on my '_NumOfDigits' variable?

Hmmm... That's a little more tricky.

I'm not sure, but this might work:

char *format[7];
char *buffer[20];
int myNumber = 123;
int digits = 9;

sprintf(format,"%%0%dd",digits);
sprintf(buffer,format,myNumber);

Basically that's building up the format string, then using that string to format the result.

I don't know if it will work or not - not got time to test it right now - but give it a go and see.

Thanks. If it's a I bit tricky I might have a play later.

IIRC you replace the width modifier with a * placeholder and add the width variable, so instead of

sprintf(buffer,"%07d",myNumber);

it's

sprintf(buffer,"%0*d", myWidth, myNumber);


Rob