43oh

# 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 on other sites

Shifts do not work like that. Why not simply multiply and add?

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

Robert

##### 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 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.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
• Blog

• #### Activity

×
• Create New...