Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. What is the equivalent of LOBYTE and HIBYTE in C#?

What is the equivalent of LOBYTE and HIBYTE in C#?

Scheduled Pinned Locked Moved C#
questioncsharphelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bouli
    wrote on last edited by
    #1

    Hello gurus, I would like to know what is the C# equivalent of the LOBYTE and HIBYTE 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.

    D 1 Reply Last reply
    0
    • B bouli

      Hello gurus, I would like to know what is the C# equivalent of the LOBYTE and HIBYTE 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.

      D Offline
      D Offline
      dnewmon
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups