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. convert byte[] to short[]

convert byte[] to short[]

Scheduled Pinned Locked Moved C#
csharpc++data-structuresperformance
6 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.
  • E Offline
    E Offline
    eyalbi007
    wrote on last edited by
    #1

    Hi all, I need to perfome simple-looking task in C#: Converting byte array to short array - meaning that if my byte array has 100 cells, my short array will have 50 cells. If it was C/C++ it was simple, using pointers. However in C# I just can't do it - I've already tried using 'unsafe' block and memory pinning using GCHandle, with no success. Another thing that bothers me in the solutions I found is that the conversion is done using memory copying, which is not necessary, since all the information is already there! I'll be grateful for any solution! Thanks, Eyal.

    L 1 Reply Last reply
    0
    • E eyalbi007

      Hi all, I need to perfome simple-looking task in C#: Converting byte array to short array - meaning that if my byte array has 100 cells, my short array will have 50 cells. If it was C/C++ it was simple, using pointers. However in C# I just can't do it - I've already tried using 'unsafe' block and memory pinning using GCHandle, with no success. Another thing that bothers me in the solutions I found is that the conversion is done using memory copying, which is not necessary, since all the information is already there! I'll be grateful for any solution! Thanks, Eyal.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      this might work

      private void foo()
      {
      byte[] b = new byte[100];
      IntPtr hnd = Marshal.AllocHGlobal(Marshal.SizeOf(b));
      Marshal.Copy(b,0,hnd,b.Length);
      short[] s = new short[50];
      Marshal.Copy(hnd,s,0,s.Length);
      Marshal.FreeHGlobal(hnd);
      }

      E 1 Reply Last reply
      0
      • L Lost User

        this might work

        private void foo()
        {
        byte[] b = new byte[100];
        IntPtr hnd = Marshal.AllocHGlobal(Marshal.SizeOf(b));
        Marshal.Copy(b,0,hnd,b.Length);
        short[] s = new short[50];
        Marshal.Copy(hnd,s,0,s.Length);
        Marshal.FreeHGlobal(hnd);
        }

        E Offline
        E Offline
        eyalbi007
        wrote on last edited by
        #3

        Hi, When using the code you suggested I get the following error: Type 'System.Byte[]' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed. Eyal.

        L 1 Reply Last reply
        0
        • E eyalbi007

          Hi, When using the code you suggested I get the following error: Type 'System.Byte[]' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed. Eyal.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          oops.. sorry.. use b.Length instead of Marshal.SizeOf(b) :) that should do the trick

          E 1 Reply Last reply
          0
          • L Lost User

            oops.. sorry.. use b.Length instead of Marshal.SizeOf(b) :) that should do the trick

            E Offline
            E Offline
            eyalbi007
            wrote on last edited by
            #5

            Hi, just wanted to say thanks - this works! the only problem I still have with this solution is that what it does is copying the entire byte[] memory to another location in the memory - what appears to be redundant, since it's already there! in C/C++ all I had to do is one pointer manipulation - can't I do it here as well? is it because the existance of garbage collector? Thanks! Eyal.

            L 1 Reply Last reply
            0
            • E eyalbi007

              Hi, just wanted to say thanks - this works! the only problem I still have with this solution is that what it does is copying the entire byte[] memory to another location in the memory - what appears to be redundant, since it's already there! in C/C++ all I had to do is one pointer manipulation - can't I do it here as well? is it because the existance of garbage collector? Thanks! Eyal.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Hi I think it has something to do with the Memory Management in .Net (which includes the garbage collector). As far as i understand it, object are not located fix in Memory. this means an Array of byte can "move" inside the memory. That's why you get an exception when you try to get the Memory Address of an Item of the Array. but you can force .Net to keep an Array at the same Location for a limited time. for this there's the fixedbr mode="hold" />i'm not quite sure about this, but i think fixing is only for reading addresses.. so i don't think it's possible to set the first entry of a short array to the address of the first item of a Byte Array. but maybe google knows more if you ask it about fixed unsafe array manipulation or something :) greets m@u

              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