I have a ushort array which holds two array items inside command.Data[]. I am able to combine those two item values and get 32Bit Big Endian value as UInt32 with the following picece of code.
UInt32 param32BitValue= (UInt32)command.Data[0] << 32 | command.Data[1];
I have another parameter which holds 4 items inside ushort array and I want to combine those four values into ulong value like
(Illustration)
UInt64 param64BitValue= (UInt64)command.Data[0] << 64 | command.Data[1] << 64 | command.Data[2] << 64 | command.Data[3];
Big or little endian format. How do I achieve this?
Your first code snippet should actually shift by 16 bits, not 32, if command.Data
really is a ushort[]
array.
uint bigEndian32 = ((uint)command.Data[0] << 16) | command.Data[1];
For 64-bit, you'd just keep shifting left by 16 bits more each time:
ulong bigEndian64 = ((ulong)command.Data[0] << 48) | (command.Data[1] << 32) | (command.Data[2] << 16) | command.Data[3];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.