Split number into individual digits and store them in an array

Hi there

I'm looking for a way to take a number and split it down to its individual digits, and then store those digits in an array. I would also like to specify the length of the array, so that larger numbers can be truncated to remove the most significant digits.

I've tried to write a function where I specify the number to split up and the number of digits I want to store, but it isn't working and I don't understand why.

Once the numbers are stored in an array I want to join them back together using bit-shifting and bitwise OR to send out to a shift register, but I haven't tried to write that bit of the code yet.

Here's what I have so far:

/
byte array[4];
byte placeloop;
byte expo;

int sevenSegOut(int displayNumber, byte numDisplays) {
  if (numDisplays == 1) { Serial.println("Boobs");
  } else {
    for(byte placeloop = numDisplays - 1; placeloop == 0; placeloop--) {
      int nplace = displayNumber;
      for (expo = placeloop; expo == 0; expo--) {
        nplace = nplace / 10;
      }
      array[placeloop] = nplace;
    }
  }
}

void setup() {

Serial.begin(9600);

}

void loop() {

sevenSegOut(4567, 4);
Serial.println(array[3]);
Serial.println(array[2]);
Serial.println(array[1]);
Serial.println(array[0]);
Serial.println(" ");
delay(1000);
}

Any help would be greatly appreciated.

Posting code correctly would be appreciated as well. Read the how to use this forum sticky post.

Try changing

 byte out = (tens << 4|units);

to

 byte out = ( (tens << 4) | units);

However the variable will not give you a number you can use, it will be in a form called binary coded decimal or BCD for short.

Sorry, that's from the code I accidentally pasted in. I've updated it to the right code now.

The value that you want to break up has a fixed upper limit on the number of characters needed to represent it as a string. Reserving that much space for a long long might cause problems, but for an int, the limit is 7 (a sign, if negative, 5 digits, and a terminating NULL). Reserve 8 just to be safe.

Then, use itoa() to convert the value to a string. Then, to convert the ASCII representation of the digit to the digit, subtract '0' from the array element.

But you haven't posted it correctly have you. Why not I asked nice enough?