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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Fast Copy\Cast of an array

Fast Copy\Cast of an array

Scheduled Pinned Locked Moved C#
csharpdata-structurestutorial
7 Posts 4 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.
  • G Offline
    G Offline
    Gilad Kapelushnik
    wrote on last edited by
    #1

    Hi I need to convert a ushort[] to byte[]. if this was C I wouldn't be asking but I wonder how to do this most efficient (new array + for) in C# Gilad. Ofcource the other way around is a must...

    G L 2 Replies Last reply
    0
    • G Gilad Kapelushnik

      Hi I need to convert a ushort[] to byte[]. if this was C I wouldn't be asking but I wonder how to do this most efficient (new array + for) in C# Gilad. Ofcource the other way around is a must...

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Try the Array.Copy method: byte[] newArray = new byte[original.Length]; Array.Copy(original, newArray, original.Length); --- b { font-weight: normal; }

      L 1 Reply Last reply
      0
      • G Gilad Kapelushnik

        Hi I need to convert a ushort[] to byte[]. if this was C I wouldn't be asking but I wonder how to do this most efficient (new array + for) in C# Gilad. Ofcource the other way around is a must...

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        go unsafe and do it like C xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

        A 1 Reply Last reply
        0
        • G Guffa

          Try the Array.Copy method: byte[] newArray = new byte[original.Length]; Array.Copy(original, newArray, original.Length); --- b { font-weight: normal; }

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Guffa wrote: Try I suggest you try it... You did not read the question correctly. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

          G 1 Reply Last reply
          0
          • L leppie

            Guffa wrote: Try I suggest you try it... You did not read the question correctly. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            In what way didn't I read the question correctly? I tried it, and the result is that it can't be copied that way. The probable reason is that you might lose data as an ushort can not safely be converted to a byte. Copying a byte array to a ushort array works fine, though. You have to loop through the array yourself and convert each value, and handle possible data loss in the conversion. One way is to simply use the lower eight bits of the value and discard the higher eight bits. --- b { font-weight: normal; }

            1 Reply Last reply
            0
            • L leppie

              go unsafe and do it like C xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

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

              I guess this is the only way... If I have an array of ushort[] and I want to "cast" (not copy) it into an array of byte[] then how do I do this: ushort[] A = new ushort[10] byte[] B; unsafe{ fixed(A){ // ??? // } } now I would like to access the first byte of the first (MSB) ushort in the array through B. what do i need to do after the "fixed" statment ? thanks. Gilad.

              L 1 Reply Last reply
              0
              • A Anonymous

                I guess this is the only way... If I have an array of ushort[] and I want to "cast" (not copy) it into an array of byte[] then how do I do this: ushort[] A = new ushort[10] byte[] B; unsafe{ fixed(A){ // ??? // } } now I would like to access the first byte of the first (MSB) ushort in the array through B. what do i need to do after the "fixed" statment ? thanks. Gilad.

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                Anonymous wrote: I want to "cast" (not copy) it into an array of byte[] Just like C, you cant cast to a "fixedsized" array. So you either have ushort[] and go to byte* or have byte[] and go to ushort*. Two array references cannot manage an array differently.

                ushort[] A = new ushort[10];
                unsafe{
                fixed(ushort* a = A){
                byte* b = (byte*) a;
                for (int i = 0; i < A.Length*2; i++)
                {
                process(b[i]);
                }
                }}

                xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

                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