Split string into 6 digits

Hello,

My code:

String in;
int input_number;
byte ones = 0, tens = 0, hundreds = 0, thousands = 0, tenthousands = 0;

void setup()
{
  // put your setup code here, to run once:
 Serial.begin(9600);  
}

void loop()
{

  // put your main code here, to run repeatedly:
in = "0158";

input_number = in.toInt();

ones = (input_number%10);
tens = ((input_number/10)%10);
hundreds = ((input_number/100)%10);
thousands = (input_number/1000);

Serial.println(ones);
delay(1000);
Serial.println(tens);
delay(1000);
Serial.println(hundreds);
delay(1000);
Serial.println(thousands);
delay(1000);
Serial.println("<testeind");
delay(1000);


}

I have code above working well. One string to int into 4 digits.

But I have a problem to expand it to 6 digits.

so the string could be “999999” and the result should be:

ones = (input_number%10);
tens = ((input_number/10)%10);
hundreds = ((input_number/100)%10);
thousands = (input_number/1000);
thenthousand = ......;
hundredthousand = ......;

I tried many things but it did not work right.
Any idea have i can do it right?

You can use ltoa() to convert a long integer (up to 9 digits) to a zero terminated character array of ASCII characters, and atol() or strtol() to do the reverse.

Please explain what you want to do with the individual digits.

Some examples:

unsigned char digits[10] = "76543210";
long number = 0;

void setup() {
  Serial.begin(9600);
  Serial.println((char) digits[2]); //the character '5'
  Serial.println((int) (digits[2] - '0')); //digit value of that character
  number = atol(digits); //convert string to number
  Serial.println(number);
  Serial.println(ltoa(number - 12345L, digits, 10)); //convert modified number back to string and print
}

void loop() {}

jremington:
You can use strtol() to convert a long integer (up to 9 digits) to a zero terminated character array of ASCII characters, and atol() to do the reverse.

Please explain what you want to do with the individual digits.

I want to use them for 7 segment displays. 6 pieces.

So I need to split up the int into seperate digits.

000000 = 6 digits.

I need the value of the seperate digits in an variable.

example: value = 789521

digit ones: 1
digit tens: 2
digit hun : 5
digit thou: 9
digit tent: 8
digit huth: 7

I can use the value to power the outputs.

If you start with a C-string (character array) like this: char x[]="12345"; it is already broken up into individual digits.

If you want to display a long integer (larger than 65535) on the screen, either use your method, or the function ltoa() as in reply #1.

in your case, value has to be declared long or unsigned long, because 6 digits don't fit into an "int".

long value = 789521L;

Done:

digit1 = (input_number%10);
digit2 = ((input_number/10)%10);
digit3 = ((input_number/100)%10);
digit4 = ((input_number/1000)%10);   
digit5 = ((input_number/10000)%10);
digit6 = (input_number/100000);

Not your way, but it works.
Thanks for your effort.
:wink:

arduinodude08:

digit1 = (input_number%10);

digit2 = ((input_number/10)%10);
digit3 = ((input_number/100)%10);
digit4 = ((input_number/1000)%10);  
digit5 = ((input_number/10000)%10);
digit6 = (input_number/100000);

The above 6-line of codes could be compacted using for loop:

  unsigned long inputNumber = 789521;
  byte digitArray[6]; //digitArray[0] contains 7, ..., digitArray[5] contains 1
  for(int i = 5; i>=0; i--)
  {
    digitArray[i] = inputNumber%10;
    inputNumber = inputNumber/10; 
  }