Test each digit of an int and convert to 4 byte array

Looking for some help here...

I have an int variable I want to display on a seven segment display, for example 1234.

How can I check and separate each digit (Thousands, Hundreds, tenths and units) and output it into a 4 byte array?

Something like:

myvar = 1234

convert to

my_array[4]

my_array[0] = 4
my_array[1] = 3
my_array[2] = 2
my_array[3] = 1

Thanks!

itoa() ?

int myval = 1234;
int my_array[4];

void setup()
{
   int tempval = myval;  // copy myval so it is not destroyed
   Serial.begin(115200);
   my_array[3] = tempval / 1000;
   tempval = tempval -  my_array[3] * 1000;
   my_array[2] = tempval / 100;
   tempval = tempval -  my_array[2] * 100;
   my_array[1] = tempval / 10;
   tempval = tempval -  my_array[1] * 10;
   my_array[0] = tempval;
   for(int n = 0; n < 4; n++)
   {
    Serial.print("my_array[");
    Serial.print(n);
    Serial.print("] = ");
    Serial.println(my_array[n]);
   }
}

void loop()
{

}

You will have to deal with thousands, hundreds and tens that are 0. I mean that, for instance, if myval = 5 the array will be {5, 0, 0, 0}.

DKWatson:
itoa() ?

Do not confuse ASCII codes for digits with the numeric values of those digits.

'1' is not the same as 1, and if you try to pretend it is, it will give you grief.

Here is some old code of mine for isolating digits of a number:

int n = 12345;  // or whatever number

int ones = n % 10;
int tens = (n / 10) % 10;
int hund = (n / 100) % 10;
int thou = (n / 1000) % 10;
int myri = (n / 10000) % 10;

Once you've isolated the digits, you can do whatever you want with them.

for (int i = 0 ; i < 4 ; i++)
{
  my_array[i] = myvar % 10 ;
  myvar /= 10 ;
}