Jeroen's Weblog

  • Archive
  • RSS

Fun with byte arrays

Some easy and fun byte array extensions

  static class ByteArrayExtensions
  {
    public static string ToHexString(this byte[] bytes)
    {
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < bytes.Length; i++)
      {
        sb.AppendFormat("{0:X2}", bytes[i]);
      }
      return sb.ToString();
    }
    
    public static byte[] Copy(this byte[] bytes)
    {
      byte[] result = new byte[bytes.Length];
      Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
      return result;
    }
  }

  public static class ByteArray
  {
    public static byte[] Concat(params byte[][] bytes)
    {
      int resultLength = 0;
      for (int i = 0; i < bytes.Length; i++)
      {
        resultLength += bytes[i].Length;
      }

      var result = new byte[resultLength];
      int resultPosition = 0;
      for (int i = 0; i < bytes.Length; i++)
      {
        Buffer.BlockCopy(bytes[i], 0, result, resultPosition, bytes[i].Length);
        resultPosition += bytes[i].Length;
      }
      return result;
    }
  }
  • 4 months ago
  • Comments
  • Permalink
  • Share
    Tweet

Recent comments

Blog comments powered by Disqus
← Previous • Next →

About

Software development, architecture and fun!

Pages

  • About Jeroen
  • Calendar

Twitter

loading tweets…

  • RSS
  • Random
  • Archive
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr