# Variable Problem

Hey guys for one of my project i need to add variables to each other like first varialble is 16 and the second is 85 i want them to be 1685 how can i do this

``````firstvar = firstvar + secondvar;
``````

Two different ways to get there depending on exactly what you are trying to do - if, as in your example, the second variable is always 2 digits then you can multiply the first by 100 and add it to the second. The second method involves treating it as two strings (or character arrays) and combining them that way. Depends on exactly what you are trying to accomplish.

The variables can be like DE or 18 or E3 but when i try the code that i wrote it doesn't work i get something like 256 by the way if it changes something these are HEX

The variables can be like DE or 18 or E3 but when i try the code that i wrote it doesn't work i get something like 256 by the way if it changes something these are HEX

In that case I think you'll need to "build" a string, and then optionally convert it back to a variable.

With hex values, you might typically have a high-byte and a low-byte. In that case, you'd shift-left the high-byte by 8 bits (or multiply by 256). But, that would obviously mess-up your 1685 decimal example. Are you sure 1685 is the "correct answer"?

NewStudent:
Hey guys for one of my project i need to add variables to each other like first varialble is 16 and the second is 85 i want them to be 1685 how can i do this

`````` int firstvar=16;
``````

int secondvar=85;

int result = firstvar*100 + secondvar;

``````

BTW:Please be aware of 'int' min and max range to avoid integer overflow in int math calculation!``````

Don't you just want to concatenate the string (Arduino: String) version of both variables:

``````int a = 65;
int b = 85;
String c = (String)a + (String) b;
Serial.print(c);
``````

Gives:
6585
on the Serial Window

first varialble is 16 and the second is 85 i want them to be 1685 how can i do this

You mean like simple primary school decimal arithmetic?

``````firstvar = (firstvar * 100) + secondvar;
``````

AWOL:
You mean like simple primary school decimal arithmetic?

``````firstvar = (firstvar * 100) + secondvar;
``````

Hmmm - I think I said that clear back in #1

Hi,
If the two variables are expressed here as HEX, then bit shift the left one by 32bits? to the left then add.

E2 and 34
E2 bit shifted becomes E2 00, then add.

# E2 00 34

E234

Now you need to write a code for it.

Tom...

TomGeorge:
Hi,
If the two variables are expressed here as HEX, then bit shift the left one by 32bits? to the left then add.

E2 and 34
E2 bit shifted becomes E2 00, then add.

# E2 00 34

E234

Now you need to write a code for it.

Tom...

Is that really a 32-bit shift?

``````void setup()
{
Serial.begin(115200);
byte hb = 0x16;
byte lb = 0x85;
word result = (hb<<8) + lb;
Serial.print("0x");
Serial.println(result,HEX); // Prints: 0x1685
}

void loop(){}
``````

Hi,
Not 32 sorry, 8bits, I just did what Oldsteve did.

``````int HHigh;
int HLow;
int Result;
int SHHigh;

void setup()
{
Serial.begin(9600);
HHigh = 0x1E;
HLow = 0x34;
SHHigh = HHigh << 8;

//=====================================
Result = SHHigh + HLow;
Serial.print (HHigh, HEX);
Serial.print("         ");
Serial.print (SHHigh, HEX);
Serial.print("   +   ");
Serial.print(HLow, HEX);
Serial.print("  =  ");
Serial.println(Result, HEX);
Serial.println("Result should be: 1E34");
//======================================
}

void loop()
{

}
``````

Tom…

TomGeorge:
Hi,
Not 32 sorry, 8bits, I just did what Oldsteve did.
Tom..

I thought it was just a typo or a temporary mental blank.

NewStudent:
by the way if it changes something these are HEX

Now, that is actually meaningless.

Variables are numbers, "Hex" is a notation for expressing numbers.

You need to go back and explain what you actually want to do, rather than waste many willing helpers' time imagining what you are trying to do when you fail to describe it.

Hi,

OldSteve:
I thought it was just a typo or a temporary mental blank.

I feel better now.

Tom...

TomGeorge:
Hi,

I feel better now.
Tom...

Know the feeling. I live on the stuff.

Are you sure it wasn’t decaffeinated? It could be the placebo effect.

Hi,

aarg:
Are you sure it wasn't decaffeinated? It could be the placebo effect.

How dare you swear in this forum.
Tom....