pad a column with 0, 1, or2 spaces depending on value of a variable [SOLVED]

I want to make justified columns when I Serial.print the contents of an array inside a for loop. I’m open to any strategies to do this. I think this code would have worked, except for one thing that has eluded me: I can’t find a way to print a string of spaces equal to (8 - len)

uint16_t var;

void setup()  {
  Serial.begin(115200);
}

void loop()  {
  const uint16_t foo[]  {7, 17, 37, 47, 67, 97, 107, 127, 137, 967, 977, 997};
  const String asc32x8 = "        ";
  uint8_t len;
  uint8_t tmp;

  for (uint8_t a = 0; a < (sizeof(foo) / sizeof(foo[0])); a++)  {
    len = 0;
    tmp = 1;
    while(tmp <= (a + 1))  {
      len++;
      tmp = (tmp << 3) + (tmp << 1);
    }
    String spaces = String(asc32x8);
    spaces.remove(7 - len);
    Serial.print(a + 1);
    Serial.print(spaces);
    Serial.println(foo[a], HEX);
  }
}

I know this must be a hideously ugly way to do this, but my programming experience is limited to a brief stint with BASIC some 30 odd years ago. I can’t believe the amount of searching and how difficult it was to find a simple way to find len (generated by the two nested loops). This was the easiest way I could find. And it has taken me all night (over 6 hours) to get this far! Eons ago, the solution would have been as simple as SPACE$(8 - LEN(STR$(var)))
So if anyone knows the function to print a string of spaces so I can finish this code (assuming that the code will then work), I would be SOOO thankful. And as I said earlier, I’m open to any other ideas/strategies that would produce the same output.
Thanks for reading.

EDIT: I had just assumed there would be a function to print a string of spaces like there is in BASIC, but as I have been learning, things are WAY different!! So I believe that there is no such function, which apparently is also true for a way to return the number of digits in a numeric variable. I spent quite some time searching Stack Overflow, and found a question that fit my needs. I was amazed to see how many answers there were and how involved and different from each other they were. I chose to use the simplest one, which is used below (the nested ‘for’ and ‘while’ loop)
As far as my post goes, I want to thank those who gave answers. After I saw their solution, it seemed obvious! Of course, thinking outside of the box is easy when you get the answer! But it was very helpful for learning problem solving, and I used their approach to put my own spin on it. Their suggestion was to forget about using a function, and just insert white space to pad the columns! So string of spaces was inserted and then trimmed using .remove() So the solution was in 4 lines:
const String asc32x8 = " "; and then later:
String spaces = String(asc32x8);
spaces.remove(7 - len); and then printed using:
Serial.print(spaces);

Serial.print( I NEED A FUNCTION THAT WILL PRINT A STRING OF (8 - LEN) SPACES);

No, you need a for loop that will iterate 8 - len times, printing one space each time.

There are several ways of doing that.
You can just use switch / case

switch(8-len) {
case 0:
break;
case 1:
Serial.print(" "); // 1 space
break;
case 2:
Serial.print("  "); // 2 spaces
break;

...

case 8:
Serial.print("        ");  // 8 spaces
break;
}

It may even be simplified

switch (8-len) {
case 8:
Serial.print(" ");
case 7:
Serial.print(" ");
case 6:
Serial.print(" ");
case 5:
Serial.print(" ");
case 4:
Serial.print(" ");
case 3:
Serial.print(" ");
case 2:
Serial.print(" ");
case 1:
Serial.print(" ");
default:
break;
}

Or you can use a for loop (as PaulS said while I was typing :slight_smile: )

for (byte i=0;i<8-len;i++) Serial.print(" ");

Or you can define an array of strings containing the spaces

char spaces[8][8] = {" ","  ","   ","    ","     ","      ","       ","        "};
Serial.print(spaces[8-len]);

(not sure about this one though)