Jump to content
Oldnotdead

Concatenate example help needed

Recommended Posts

________________________________________________________________ 

 

I am using a launchpad with an msp430g2553 running Energia 17. 

I am trying to concatenate 4 integers into a single  number

 

int a = 48;

int b = 49;

int c = 50;

int d = 51;

 

I am trying to get this  result : int e = 48495051,

 

the result I get when running the following code is 18231,

Please tell me where I have gone wrong. Can anyone provide a working example of

Code which accomplishes the result I am trying to achieve.

Thanks in advance for any suggestions.

 

 

void setup()

{

 Serial.begin(9600); 

}

void loop()

{

 int a = 48;

 int b = 49;

 int c = 50;

 int d = 51;

 unsigned long int e = a | (b << 8) | (c << 16) | (d << 24);

 Serial.println(e);

 delay(1000);

}

 

 

?

 

 

 

 

 

Share this post


Link to post
Share on other sites

Thank you for your help, I put together this sketch and it works okay now.

 

void setup()

{

 Serial.begin(9600);

}

void loop()

{

  long a = 48;

  long b = 49;

  long c = 50;

  long d = 51;

  long e;

  e=(a*1000000)+(b*10000)+(c*100)+d;

  Serial.println(e);

  delay(1000);

 

}

?

Share this post


Link to post
Share on other sites

Shifting by 8 will "multiply" your numbers by 256, while your intention was (probably) to multiply by 100.

 int a = 0x48;
 int b = 0x49;
 int c = 0x50;
 int d = 0x51;
 unsigned long int e = a | (b << 8) | (c << 16) | (d << 24);
 // e == 0x51504948

since 0x100 is 256 this works for hexadecimal numbers, but for decimal numbers, you'll have to do multiplication.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×