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. Extracting bytes from a 32-bit value

Extracting bytes from a 32-bit value

Scheduled Pinned Locked Moved C#
csharpquestion
3 Posts 3 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.
  • H Offline
    H Offline
    H M J
    wrote on last edited by
    #1

    What would be a good C# programming manner to extract from a 32-bit value the different bytes (MSB to LSB) in big or little endian format?

    C L 2 Replies Last reply
    0
    • H H M J

      What would be a good C# programming manner to extract from a 32-bit value the different bytes (MSB to LSB) in big or little endian format?

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Do an && to strip the bits you don't want, and shift them down with >>

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      1 Reply Last reply
      0
      • H H M J

        What would be a good C# programming manner to extract from a 32-bit value the different bytes (MSB to LSB) in big or little endian format?

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        There is an alternative, that may be interesting when many variables need to available in both packed and unpacked form. Using structs you can obtain a "union" effect in C# in the following way:

        using System.Runtime.InteropServices; // StructLayout

        [StructLayout(LayoutKind.Explicit)]
        public struct Overlay {
        [FieldOffset(0)]public uint u32;
        [FieldOffset(0)]public byte u8_0;
        [FieldOffset(1)]public byte u8_1;
        [FieldOffset(2)]public byte u8_2;
        [FieldOffset(3)]public byte u8_3;
        }
        public override void Run() {
        Overlay overlay=new Overlay();
        overlay.u32=0x12345678;
        log("lo to hi: "+overlay.u8_0.ToString("X2")+" "+overlay.u8_1.ToString("X2")+
        " "+overlay.u8_2.ToString("X2")+" "+overlay.u8_3.ToString("X2"));
        }

        On a little-endian machine (such as Intel x86) the Run method will log "lo to hi: 78 56 34 12" Be careful though, you are responsible for the offset values ! :) -- modified at 11:54 Sunday 4th February, 2007

        Luc Pattyn

        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