How can I parse an integer to an array of single digits in arduino?

Friends,
I have been through the ringer with this one and i can't seem to get anything to come together. This not homework or anything. I am trying to get an 8 character, 7 segment led working as a counter.

I would like to convert an int into an array with each digit as an array value. something like:

int asdf = 12345678;
array qwer[8];

then some code that would render:

qwer(0) = 1
qwer(1) = 2
etc.

I have tried some modulo code and some atoi code from the C++ forums and they don't seem to work the way I have been doing it, I guess.

Any assistance woud be much appreciated, thanks.

You can use sprintf to get a text representation of a number. Note though that the example you've given is too big to fit in an int - use long instead.

Show us the code you've tried - you might be close already...

int asdf = 12345678;

Unless you've got a Due, that's not going to fit.

What you want is fairly simple decimal arithmetic - useful operators are "/" and "%".

The easiest approach, depends on whether you know how long the number is. You also need to be clear, if you want the actual one-digit number, or the character representing that one-digit number.

Here is one method.

int  x = 12345 ; // this is the number you want to convert
int  v[8] ;          // this is the result.   The least significant digit goes in position 0
int i=0 ;            // index
int y ;
while ( x > 0 )
{
    y=x/10 ;
    v[i] = x-(10*y) ;
    x=y ;
    i++ ;
}

UGH. I think the root of my issues was that i was using int instead of long. Thanks for the help and code. i will take another pass tonight and hopefully make something useful to share out.

rdur, (spoken, it sounds almost like "harder" when you're heavy lifting in a crew)

It's okay to use the functions but know what's behind them, in the case of text it is ACSII codes.
Hang in there, a bit of knowing will remove doubts and hesitations. Get past the mystery all you can!

Note: cripes, by the time I got this together there's 4 other replies already!

int asdf = 12345678;
array qwer[8];

then some code that would render:

qwer(0) = 1
qwer(1) = 2

The number in int asdf is 16 binary bits that equals decimal 12345678 except that decimal 12345678 won't fit in a 16 bit integer! Those can hold -32768 to 32767. When you try more they overflow and that usually makes a code bug which is why it's important to know the limits, this is good info not trivia. 12345678 take a long to hold, just so you know when you need to deal with big integers.

Text is a whole other ballgame. All text chars do hold binary values. What gets shown, otoh, are the corresponding ASCII text characters. Before "look it up on the internet" we used books and every programming book worth having had an ASCII Table in the front or back. It helps to know how the mess is organized.

You can get ASCII into char variables this way:

char textLetter = '0'; // now textLetter == decimal 48

and you can change them or evaluate through math, oh yeah.

textLetter++; // now textLetter == 49 == '1'

textLetter = '9';
byte theNumber = textLetter - '0'; // theNumber == 9, only works right for text '0' through '9'

And always remember that with char array text strings, there has to be a NULL (== 0) char at the end unless you're using your own code that knows where to stop translating. The NULL marks the end for print and most string functions.

But, just for fun, this is tested and you can load it into Arduino and mess with it or cut&paste lines:

#include <stdio.h> // to get sprintf()
#include <stdlib.h> // to get itoa()

void setup( void )
{
  Serial.begin( 9600 );

  int  asdf = 12345;
  int  saveVal = asdf;
  char qwer[ 8 ]; // I give it a little extra room because of the last two examples

  int  divider = 10000; // work digits high to low, left to right
  byte i = 0; // what char in qwer to fill next
  byte firstNonZero = 0; // to keep out leading zeros

  while ( divider > 0 )
  {
    if ( firstNonZero == 0 )
    {
      if ( asdf >= divider )
     {
       firstNonZero = 1;
     } 
    }

    if ( firstNonZero > 0 )
    { 
      qwer[ i++ ] = '0' + ( asdf / divider ); // ++ increments AFTER use 
      asdf %= divider; // asdf is now the remainder of the division
    }
    
    divider /= 10; // move to the next digit
  }
  
  qwer[ i ] = 0; // add the NULL string terminator
  
  Serial.println( "\nbehind the text -- numbers" ); // \n is ASCII newline
  Serial.println( "hang in there!\n" ); // \n is ASCII newline
  Serial.print( "the original int is " );
  Serial.println( saveVal );
  Serial.print( "the roll your own converted text is " );
  Serial.println( qwer );

  sprintf( qwer, "%d", saveVal );
  Serial.print( "\nthe sprintf converted text is " );
  Serial.println( qwer );
  
  itoa( saveVal, qwer, 10 ); // 10 is the radix for decimal result
  Serial.print( "\nthe itoa converted text is " );
  Serial.println( qwer );
}

void loop( void )
{
}

Here's the AVR LibC (what Arduino IDE uses) library page. Serious-up, bookmark this page!
This IS a final-type go to reference for Arduino. The only "moar" is the source codes in your IDE and even then, these pages have explanations you won't find so easy in the source if at all.
http://www.nongnu.org/avr-libc/user-manual/modules.html

Here is where you will find the stdio library:
http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html

And the major part of what you need to know to convert with sprintf, in case you think MY bit of code is long.
But then, sprintf (part of the vprintf family) formats -everything-. It IS good to know eventually.
Just know that "1 line of code" can take a good bit of learning and may generate a lot of actual code.
Look into the format specifiers section, it won't fit this post!

A different approach:

  char v [6] ;
  for (byte j = 0 ; j < 5 ; j++)
    v[j] = '0' ;
  v [5] = 0 ;

  byte ind = 0 ;
  byte i = 4 ;

  while (x > 0)
  {
    if (x >= 40000u)
    {
      v[ind] += i ;
      x -= 40000u ;
    }
    i >>= 1 ;
    if (i == 0)
    {
      ind ++ ;
      i = 8 ;
      x += x >> 2 ;
    }
    else 
      x <<= 1 ;
  }

No divisions or remainders needed...

I wouldn't mind seeing the whole example sketch.

http://playground.arduino.cc/LEDMatrix/Max7219#.UyzxAPldW_g

max7219_tutorial_pre_c.pdf (186 KB)