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. Fastest way to convert from/to byte array

Fastest way to convert from/to byte array

Scheduled Pinned Locked Moved C#
data-structures
2 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.
  • W Offline
    W Offline
    Werdna
    wrote on last edited by
    #1

    I need to convert various datatypes (mostly doubles, bytes and strings) to byte array and then read it back. I need to read it the fastest way possible and it will be read thousands of times and written only once. What I use now is BinaryReader/Writer like so: MemoryStream stream = new MemoryStream() BinaryWriter writer = new BinaryWriter(stream) .. loop .. writer.write(byte) writer.write(double) writer.write(string) ... return stream.ToArray(); I don't care how fast writing is. Then I need to read the data: byte[] data = ... MemoryStream stream = new MemoryStream(data); BinaryReader reader = new BinaryReader(stream); while (stream.Position < stream.Length) { byte b = reader.ReadByte(); switch (b) { // depending on value of b read strings, doubles or bytes } } This approach doesn't seem to be too fast. I'm wondering if there is a faster way to do that. Thanks.

    H 1 Reply Last reply
    0
    • W Werdna

      I need to convert various datatypes (mostly doubles, bytes and strings) to byte array and then read it back. I need to read it the fastest way possible and it will be read thousands of times and written only once. What I use now is BinaryReader/Writer like so: MemoryStream stream = new MemoryStream() BinaryWriter writer = new BinaryWriter(stream) .. loop .. writer.write(byte) writer.write(double) writer.write(string) ... return stream.ToArray(); I don't care how fast writing is. Then I need to read the data: byte[] data = ... MemoryStream stream = new MemoryStream(data); BinaryReader reader = new BinaryReader(stream); while (stream.Position < stream.Length) { byte b = reader.ReadByte(); switch (b) { // depending on value of b read strings, doubles or bytes } } This approach doesn't seem to be too fast. I'm wondering if there is a faster way to do that. Thanks.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      See the BitConverter class documentation in the .NET Framework SDK.

      Microsoft MVP, Visual C# My Articles

      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