What is the equivalent of LOBYTE and HIBYTE in C#?
-
Hello gurus, I would like to know what is the C# equivalent of the
LOBYTE
andHIBYTE
Win32 macros? I wish to get the low byte and high byte from a C#ushort
. How can I do that? Thanks for your help. Best regards. There is no spoon.Well the C++ macros are defined as follows:
#define MAKEWORD(a, b) ((WORD)(((BYTE)((DWORD_PTR)(a) & 0xff)) | ((WORD)((BYTE)((DWORD_PTR)(b) & 0xff))) << 8)) #define MAKELONG(a, b) ((LONG)(((WORD)((DWORD_PTR)(a) & 0xffff)) | ((DWORD)((WORD)((DWORD_PTR)(b) & 0xffff))) << 16)) #define LOWORD(l) ((WORD)((DWORD_PTR)(l) & 0xffff)) #define HIWORD(l) ((WORD)((DWORD_PTR)(l) >> 16)) #define LOBYTE(w) ((BYTE)((DWORD_PTR)(w) & 0xff)) #define HIBYTE(w) ((BYTE)((DWORD_PTR)(w) >> 8))
Looks ugly and complex but its not as hard as it looks. Most of the casting can be removed actually. C++ macros don't have parameter types so their casting to ensure all the integers are the correct size before performing the math on them. I used the UInt32, UInt16 and Byte types to be explicit. So I created the C# equivalent:class ByteAccess { public static UInt32 MakeLong(UInt16 high, UInt16 low) { return ((UInt32)low & 0xFFFF) | (((UInt32)high & 0xFFFF) << 16); } public static UInt16 MakeWord(byte high, byte low) { return (UInt16)(((UInt32)low & 0xFF) | ((UInt32)high & 0xFF) << 8); } public static UInt16 LoWord(UInt32 nValue) { return (UInt16)(nValue & 0xFFFF); } public static UInt16 HiWord(UInt32 nValue) { return (UInt16)(nValue >> 16); } public static Byte LoByte(UInt16 nValue) { return (Byte)(nValue & 0xFF); } public static Byte HiByte(UInt16 nValue) { return (Byte)(nValue >> 8); } }
Enjoy. David