Solved

Join 2 words to make a dword

Posted on 2004-08-26
11
756 Views
Last Modified: 2010-04-15
I am reading data back from a microcontroller using modbus.  I need to join two words together to make a dword.

I have both word values stored in variables in C, as below:

var1 = 15
var2 = 25

I need to join them so var3 has the value 1525

I also need to be able to split them back up into 2 words.

Are there standard C functions for performing these operations? cast 2 words to a dword?
0
Comment
Question by:richardoc
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 11903939
Pls explain? I assume 15 is 0x15, that fits a byte. So does 0x25. Joint, they for a word 0x1525, if you use
    var3= (var1<<8)|var2

Other operations are comparable (but always bitwise).
0
 

Author Comment

by:richardoc
ID: 11904013
Basically I have 2 integers, and I need to join them to make a long . I used 15 and 25 just as examples, but the end value must be <value1><value2>

Does this make sense?
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11904079
How About:

int num1=15,num2=25;

long longnum=(num1*100)+num2;
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11904105
>I also need to be able to split them back up into 2 words.

int num1,num2;

num1=longnum%100;

num2=longnum/100;
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11904129
Oh My God,WHat am i typing?

I need some sleep.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 46

Accepted Solution

by:
Sjef Bosman earned 125 total points
ID: 11904200
Standard way is using bitwise operators. To join the two vars (portable):

    unsigned long var3;
    unsigned int var1, var2;

    var3= ((unsigned long)var1<<16) | var2

To split them once again:
    var1= var3<<16;
    var2= var3&0xFFFF;
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 11904371
small bug [notice right shift]:

To split them once again:
   var1= var3>>16;
   var2= var3&0xFFFF;


0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 11904490
I might need some sleep as well... :(
0
 
LVL 22

Expert Comment

by:grg99
ID: 11905689
Your question is ambiguous.

Most computers nowdays are binary base, so when you put two bytes together, you end up with 256 times the high byte plus the low byte.

From your example, it looks like you expect to get 100 times the high part plus the low part.

Please clarify, do you want to combine the digits decimally, or binary-wise?

Regards,


grg99

0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 11906259
Your question is also rather schizo. You seem to know well about words and dword (assembler stuff) but seems not to be familiar with more or less elementary bit-fiddling operators or assembler. If my solution above is what you were after. Or am I mistaken?? Or is this a trick question?
0
 

Author Comment

by:richardoc
ID: 11913235
I'm a high level programmer working with assembly programmers, so I know some terms but don't neccessarily understand them :)  Bad I know, but hey, you gotta start somehow.  sjef_bosman's answer did the job nicely. Thanks all.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
My eclipse editor won't start ? 4 144
Passing command line arguments in C 10 85
How to jump to matching brace in eclipse editor ? 1 264
How to learn Linux? 10 43
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now