Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,859 questions

51,780 answers

573 users

How to read binary file into a byte array in C#

1 Answer

0 votes
using System;
using System.IO;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] bytes;

            bytes = File.ReadAllBytes("d:\\image.gif");

            PrintByteArray(bytes);
        }
        static void PrintByteArray(byte[] bytes)
        {
            var sb = new StringBuilder("byte[] = ");

            foreach (var b in bytes)
                sb.Append(b + ", ");

            Console.WriteLine(sb.ToString());
        }
    }
}

/*
run:
   
byte[] = 71, 73, 70, 56, 57, 97, 8, 0, 10, 0, 112, 0, 0, 33, 249, 4, 0, 0, 0, 0,
 0, 44, 0, 0, 0, 0, 8, 0, 10, 0, 135, 0, 0, 0, 0, 0, 51, 0, 0, 102, 0, 0, 153, 0
, 0, 204, 0, 0, 255, 0, 43, 0, 0, 43, 51, 0, 43, 102, 0, 43, 153, 0, 43, 204, 0,
 43, 255, 0, 85, 0, 0, 85, 51, 0, 85, 102, 0, 85, 153, 0, 85, 204, 0, 85, 255, 0
, 128, 0, 0, 128, 51, 0, 128, 102, 0, 128, 153, 0, 128, 204, 0, 128, 255, 0, 170
, 0, 0, 170, 51, 0, 170, 102, 0, 170, 153, 0, 170, 204, 0, 170, 255, 0, 213, 0,
0, 213, 51, 0, 213, 102, 0, 213, 153, 0, 213, 204, 0, 213, 255, 0, 255, 0, 0, 25
5, 51, 0, 255, 102, 0, 255, 153, 0, 255, 204, 0, 255, 255, 51, 0, 0, 51, 0, 51,
51, 0, 102, 51, 0, 153, 51, 0, 204, 51, 0, 255, 51, 43, 0, 51, 43, 51, 51, 43, 1
02, 51, 43, 153, 51, 43, 204, 51, 43, 255, 51, 85, 0, 51, 85, 51, 51, 85, 102, 5
1, 85, 153, 51, 85, 204, 51, 85, 255, 51, 128, 0, 51, 128, 51, 51, 128, 102, 51,
 128, 153, 51, 128, 204, 51, 128, 255, 51, 170, 0, 51, 170, 51, 51, 170, 102, 51
, 170, 153, 51, 170, 204, 51, 170, 255, 51, 213, 0, 51, 213, 51, 51, 213, 102, 5
1, 213, 153, 51, 213, 204, 51, 213, 255, 51, 255, 0, 51, 255, 51, 51, 255, 102,
51, 255, 153, 51, 255, 204, 51, 255, 255, 102, 0, 0, 102, 0, 51, 102, 0, 102, 10
2, 0, 153, 102, 0, 204, 102, 0, 255, 102, 43, 0, 102, 43, 51, 102, 43, 102, 102,
 43, 153, 102, 43, 204, 102, 43, 255, 102, 85, 0, 102, 85, 51, 102, 85, 102, 102
, 85, 153, 102, 85, 204, 102, 85, 255, 102, 128, 0, 102, 128, 51, 102, 128, 102,
 102, 128, 153, 102, 128, 204, 102, 128, 255, 102, 170, 0, 102, 170, 51, 102, 17
0, 102, 102, 170, 153, 102, 170, 204, 102, 170, 255, 102, 213, 0, 102, 213, 51,
102, 213, 102, 102, 213, 153, 102, 213, 204, 102, 213, 255, 102, 255, 0, 102, 25
5, 51, 102, 255, 102, 102, 255, 153, 102, 255, 204, 102, 255, 255, 153, 0, 0, 15
3, 0, 51, 153, 0, 102, 153, 0, 153, 153, 0, 204, 153, 0, 255, 153, 43, 0, 153, 4
3, 51, 153, 43, 102, 153, 43, 153, 153, 43, 204, 153, 43, 255, 153, 85, 0, 153,
85, 51, 153, 85, 102, 153, 85, 153, 153, 85, 204, 153, 85, 255, 153, 128, 0, 153
, 128, 51, 153, 128, 102, 153, 128, 153, 153, 128, 204, 153, 128, 255, 153, 170,
 0, 153, 170, 51, 153, 170, 102, 153, 170, 153, 153, 170, 204, 153, 170, 255, 15
3, 213, 0, 153, 213, 51, 153, 213, 102, 153, 213, 153, 153, 213, 204, 153, 213,
255, 153, 255, 0, 153, 255, 51, 153, 255, 102, 153, 255, 153, 153, 255, 204, 153
, 255, 255, 204, 0, 0, 204, 0, 51, 204, 0, 102, 204, 0, 153, 204, 0, 204, 204, 0
, 255, 204, 43, 0, 204, 43, 51, 204, 43, 102, 204, 43, 153, 204, 43, 204, 204, 4
3, 255, 204, 85, 0, 204, 85, 51, 204, 85, 102, 204, 85, 153, 204, 85, 204, 204,
85, 255, 204, 128, 0, 204, 128, 51, 204, 128, 102, 204, 128, 153, 204, 128, 204,
 204, 128, 255, 204, 170, 0, 204, 170, 51, 204, 170, 102, 204, 170, 153, 204, 17
0, 204, 204, 170, 255, 204, 213, 0, 204, 213, 51, 204, 213, 102, 204, 213, 153,
204, 213, 204, 204, 213, 255, 204, 255, 0, 204, 255, 51, 204, 255, 102, 204, 255
, 153, 204, 255, 204, 204, 255, 255, 255, 0, 0, 255, 0, 51, 255, 0, 102, 255, 0,
 153, 255, 0, 204, 255, 0, 255, 255, 43, 0, 255, 43, 51, 255, 43, 102, 255, 43,
153, 255, 43, 204, 255, 43, 255, 255, 85, 0, 255, 85, 51, 255, 85, 102, 255, 85,
 153, 255, 85, 204, 255, 85, 255, 255, 128, 0, 255, 128, 51, 255, 128, 102, 255,
 128, 153, 255, 128, 204, 255, 128, 255, 255, 170, 0, 255, 170, 51, 255, 170, 10
2, 255, 170, 153, 255, 170, 204, 255, 170, 255, 255, 213, 0, 255, 213, 51, 255,
213, 102, 255, 213, 153, 255, 213, 204, 255, 213, 255, 255, 255, 0, 255, 255, 51
, 255, 255, 102, 255, 255, 153, 255, 255, 204, 255, 255, 255, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 8, 26, 0, 149, 45, 219, 183, 44, 218, 190, 131, 6, 15, 42, 36,
 184, 80, 97, 194, 134, 16, 35, 74, 156, 72, 81, 98, 64, 0, 59,

*/


answered Mar 12, 2015 by avibootz
edited Mar 12, 2015 by avibootz

Related questions

1 answer 166 views
2 answers 247 views
2 answers 282 views
1 answer 158 views
1 answer 351 views
1 answer 213 views
...