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. how to convert a short array to a byte array?

how to convert a short array to a byte array?

Scheduled Pinned Locked Moved C#
data-structurestutorialquestion
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.
  • B Offline
    B Offline
    bouli
    wrote on last edited by
    #1

    Hello gurus, I'd like to know how to convert an array of shorts (or what ever) to an array of bytes to send on a socket? And vice versa... Can anybody show me a code snipet please? Best regards. Fred.

    There is no spoon.

    S J 2 Replies Last reply
    0
    • B bouli

      Hello gurus, I'd like to know how to convert an array of shorts (or what ever) to an array of bytes to send on a socket? And vice versa... Can anybody show me a code snipet please? Best regards. Fred.

      There is no spoon.

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      Take a look at the MSDN topic of the Array.ConvertAll method.


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      1 Reply Last reply
      0
      • B bouli

        Hello gurus, I'd like to know how to convert an array of shorts (or what ever) to an array of bytes to send on a socket? And vice versa... Can anybody show me a code snipet please? Best regards. Fred.

        There is no spoon.

        J Offline
        J Offline
        J Dunlap
        wrote on last edited by
        #3

        Sounds like Buffer.BlockCopy() is what you need. Each short value will be split in half to become 2 byte values in the byte array.

        //'values' is the name of the array of short values
        byte[] buffer=new byte[values.Length*2];
        Buffer.BlockCopy(values,0,buffer,0,buffer.Length);
        //'buffer' now holds the short values as bytes

        C# / DHTML / VG.net / MyXaml expert currently looking for work![^]

        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