 # "Grouping" integer values from an array

Hi guys, newladybye here :) In my project i receive a byte array of 12 elements 3 of these elements are integer values ( from 0 to 9) that represent a code for another purpose. For example data= 5 ,data= 6, data= 3;

i'd like to "group" them up to create a longer integer that represents this code ( in this case code=563) I'm a newbie in programming so... how can i do that?

You could add '0' to each one, and add a character 0 to the end of the array - thus turning it into a string.

So majenko you mean something like : code1 =data + '0'; code2 =data + '0'; code3 =data + '0'; code4='0'; String newstring = code1+code2+code3+code4 ;

?

@HazardMinds i looked for atoi() but it seems to do the opposite of my purpose . It converts a string to integer, i need to take integers and create a longer integer

Hell no. Stay away from String like the plague!

It's as simple as:

``````data += '0';
data += '0';
data += '0';
data = 0;
Serial.println((char *)data);
``````

Add '0' to each entry of the array. Remember, a character is just a human representation of a number, so just add a pre-set number to each entry so it represents the right character (in this case '0' - the ASCII character 0 not the integer 0). Slap 0 on the end (C string terminating NULL character) and treat it like a C string (cast it to char *).

majenko: Hell no. Stay away from String like the plague!

It's as simple as:

``````data += '0';
data += '0';
data += '0';
data = 0;
Serial.println((char *)data);
``````

Add '0' to each entry of the array. Remember, a character is just a human representation of a number, so just add a pre-set number to each entry so it represents the right character (in this case '0' - the ASCII character 0 not the integer 0). Slap 0 on the end (C string terminating NULL character) and treat it like a C string (cast it to char *).

Ok, thank you, now it's more clear...... but what about making comparisons with this ?

As i represented in my first post , i can make comparisons doing : if ( data == 563 ) ............... or if (data == "563") ................... ?

``````int newint = data * 100 + data * 10 + data;
``````

You end up with a C string, so you can use all the C string functions, such as

``````if (strcmp(data, "563") == 0) {
// yes, they match
}
``````

And you can convert to an integer with atoi:

``````int val = atoi(data);
``````

Or if you just want an integer you can do as David suggests and calculate it from the values by multiplying each digit by 10n.

Thank you all guys!!!! i will try both solutions and then use the one that has best perfomances :)

Thank you majenko, David and of course all others that have partecipated this discussion. I think i should study more of C language ..... :)