Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi there,

 

As you can see this is my first post in these forums. I've only been working with C# for a week or so, but I'm getting there...

 

Can anyone suggest an easy way to turn two 16 bit (short) values into one 32 bit (int) value. Obviously I'm not wanting to just add them together here...

 

Basically I am reading registers from an industrial controller (using ModbusTCP). The slave controller stores a 32 bit value in two 16 bit registers. I now need to put these back together.

 

My solution at the moment is to read the value of the least significent 16 register into a 32 bit (int) register and then copy the bits from the other 16 bit register into the higher bits of the 32 bit register. I can't see there's a built in method which will do this.

 

Any help would be greatly appreciated.

 

Thanks,

 

Rob.

Posted
You could shift the bits of one short to the left bu 16 bits then add them together

ushort s1 =0xabcd, s2 = 0x1234;
int i1 = (s1 << 16) + s2;    //i1 now equals 0xabcd1234

 

That's great, cheers m8 !

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

×
×
  • Create New...