Rob100 Posted June 27, 2004 Posted June 27, 2004 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. Quote
Administrators PlausiblyDamp Posted June 27, 2004 Administrators Posted June 27, 2004 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 Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Rob100 Posted June 27, 2004 Author Posted June 27, 2004 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 ! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.