Go Down

### Topic: simple concatenation (Read 6229 times)previous topic - next topic

##### Apr 21, 2009, 09:10 pm
Hi...

I have an array, and I wish to take values from multiple cells and combine them into 1 value.

I obviously have the syntax incorrect, how can I concatenate the 3 values below into a single value...

Here is the sample code...

//create 1 number
controlNumber = 0;
controlNumber =  (userInput[7],BYTE) && (userInput[6],BYTE) && (userInput[5],BYTE);
Serial.print(controlNumber);  // fetch the final results from the array

#### AlphaBeta

#1
##### Apr 21, 2009, 09:24 pm
Why not use the good old addition?

#2
##### Apr 21, 2009, 09:26 pm
Just wanted to see if this could be done... Can you suggest a way to do this?

#### halley

#3
##### Apr 21, 2009, 09:47 pm
What does the input array represent?  Decimal digits in ASCII?  Decimal digit values?  Bytes that belong to a larger multi-byte value?

#4
##### Apr 21, 2009, 09:52 pm
decimal digit values...

But does it matter?

In that, I might want to "concatenate" other types as well - as long as the data types correspond.

#### halley

#5
##### Apr 21, 2009, 10:02 pm
Well, there's a big difference in the operations that "concatenate" numbers.

ASCII digits:
Code: [Select]
`int total = 0;for (int i = 0; i < number_of_digits; i++)   total = total*10 + (digits[i] - '0');`

Decimal digits:
Code: [Select]
`int total = 0;for (int i = 0; i < number_of_digits; i++)   total = total*10 + digits[i];`

Bytes, matching byte order:
Code: [Select]
`union { long total; byte a,b,c,d; };a = bytes[0];b = bytes[1];c = bytes[2];d = bytes[3];`

Bytes, inverse byte order:
Code: [Select]
`union { long total; byte a,b,c,d; };a = bytes[3];b = bytes[2];c = bytes[1];d = bytes[0];`

There are probably fifty ways I could code, depending on what you have.  The original question is just too vague to be meaningful.

#6
##### Apr 22, 2009, 03:52 am
Ok.. I have tried a lot of variations with the code examples you provided and could not get them to produce what I need. Here is more info.
I have an array with 3 values:
(userInput[5],BYTE) = 4;
(userInput[6],BYTE) = 3;
(userInput[7],BYTE) = 9;

int controNumber = 0;

I would like to assign the value of 439 to controlNumber.

If I have another array,

(userInput[5],BYTE) = 6;
(userInput[6],BYTE) = 7;
(userInput[7],BYTE) = '';

I would like to assign the value of 67 to controlNumber.

I hope that clarrifies things..

#7
##### Apr 22, 2009, 08:11 pm
Hi... Haven't heard from anyone - wondering can this be done?

#### AlphaBeta

#8
##### Apr 22, 2009, 09:14 pm
The code snippet from halley works fine.

Quote

byte userInput[3] = { 4 , 3 , 9 };

void setup(){
Serial.begin(9600);
int total = 0;
for (int i = 0; i<3; i++){ total = total*10 + userInput; }
Serial.println(total);
}

void loop(){/*nothing*/}

This prints 439.

#9
##### Apr 22, 2009, 10:38 pm
Hi...

Thanks for verifying the code !!!...

I must have typed something incorrectly...

#10
##### Apr 22, 2009, 11:01 pm
Ok... I've gotten 2 of the examples working and now am trying to see how the union one works...  I typed in the following code but it did not result in any statements being printed... Any ideas?

//union ASCII digits: Code:
void setup(){
byte userInput[4] = {'2','4','2','4'};
union { long total; byte a, b, c, d; };
a = userInput[0];
b = userInput[1];
c = userInput[2];
d = userInput[3];

Serial.println(total);
Serial.println(total,BYTE);
Serial.println(total,DEC);
}

void loop(){}

#11
##### Apr 22, 2009, 11:07 pm
Also, come to think of it, how do I concatenate alpha characters...

I tried the following but it did not work..

//ASCII character digits: Code:
byte userInput[8] = { '0', '0', '0', '0', '0', 'E' , 'B' , 'D' };

void setup(){
Serial.begin(9600);
char total;
for (int i = 5; i<8; i++){
total = total + (userInput);
}
Serial.println(total);
}

void loop(){}

#12
##### Apr 22, 2009, 11:48 pm
Hi again... I found a bug with the process...

Since I am collecting data dynamically, I don't always get 3 digits, maybe 1, 2 or 3...

So, running this sample code, my output becomes : 132 and 430
when I should be getting: 132 and 43...

here is the code:

byte userInput[3] = {NULL,NULL,NULL};

int total = 0;
void setup(){
Serial.begin(9600);

userInput[1] = NULL;
userInput[2] = NULL;
userInput[3] = NULL;

userInput[1] = 1;
userInput[2] = 3;
userInput[3] = 2;
total = 0;
for (int i = 1; i<4; i++){ total = total*10 + userInput; }
Serial.println(total);

userInput[1] = NULL;
userInput[2] = NULL;
userInput[3] = NULL;

userInput[1] = 4;
userInput[2] = 3;
total = 0;
for (int i = 1; i<4; i++){ total = total*10 + userInput; }
Serial.println(total);

}

void loop(){/*nothing*/}

#13
##### Apr 22, 2009, 11:52 pm
ok, I think I handled that last problem by wrapping an if statement.

for (int i = 1; i<4; i++){
if (userInput != NULL){
total = total*10 + userInput;
}

But, I still can't get the alpha concatenation to work...

#### tomjung09

#14
##### Oct 28, 2009, 11:50 pm
One of my teammates figured it out.  Apparently the timing is a little off, why it is, I am not sure.  We added 4 to each of our default times and now it works properly.

Go Up

Please enter a valid email to subscribe

To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy