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. Safe version of unsafe code

Safe version of unsafe code

Scheduled Pinned Locked Moved C#
csharpc++helpannouncement
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.
  • C Offline
    C Offline
    cheesepirate
    wrote on last edited by
    #1

    I'm getting to grips with C#, but one thing still escapes me If I have a number, 0x12345678, and I need to reorder it for some reason to 0x56781234, I could do this in C/C++ int i = 0x12345678; short *ps = (short*)&i; short s; s = ps [0]; ps [0] = ps [1]; ps [1] = s; i is then 0x56781234. This sort of problem also pops up when a file has some binary data encoded in such a way that 3 bytes are used to represent a 4 byte int (PowerPoint does this if I remember correctly) All attempts I've tried at this so far have resulted in unsafe code. I'd prefer a safe way, if this is possible. Cheers

    M A 2 Replies Last reply
    0
    • C cheesepirate

      I'm getting to grips with C#, but one thing still escapes me If I have a number, 0x12345678, and I need to reorder it for some reason to 0x56781234, I could do this in C/C++ int i = 0x12345678; short *ps = (short*)&i; short s; s = ps [0]; ps [0] = ps [1]; ps [1] = s; i is then 0x56781234. This sort of problem also pops up when a file has some binary data encoded in such a way that 3 bytes are used to represent a 4 byte int (PowerPoint does this if I remember correctly) All attempts I've tried at this so far have resulted in unsafe code. I'd prefer a safe way, if this is possible. Cheers

      M Offline
      M Offline
      Mardawi
      wrote on last edited by
      #2

      the only way i can recall now is to put your data in a string then manipulated it the way you want. after that cast it back. i don't think there could be another way becuase you are accessing the memory directly here. good luck!

      1 Reply Last reply
      0
      • C cheesepirate

        I'm getting to grips with C#, but one thing still escapes me If I have a number, 0x12345678, and I need to reorder it for some reason to 0x56781234, I could do this in C/C++ int i = 0x12345678; short *ps = (short*)&i; short s; s = ps [0]; ps [0] = ps [1]; ps [1] = s; i is then 0x56781234. This sort of problem also pops up when a file has some binary data encoded in such a way that 3 bytes are used to represent a 4 byte int (PowerPoint does this if I remember correctly) All attempts I've tried at this so far have resulted in unsafe code. I'd prefer a safe way, if this is possible. Cheers

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        How 'bout using the shift operators? int i = 0x12345678; i = (i << 16) + (i >> 16);

        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