Return to Snippet

Revision: 39966
at January 24, 2011 06:59 by kyrathaba


Initial Code
namespace Kyrathasoft.ArraysAndCollections.WorkingWithByteArrays {

    using System;
    using System.IO;
    using System.Diagnostics;

    public static class clsWorkWithByteArrays {

        public static byte[] ReadByteArrayFromFile(string fileName) {
            byte[] buff = null;
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            long numBytes = new FileInfo(fileName).Length;
            buff = br.ReadBytes((int)numBytes);
            return buff;
        }

        public static byte[] StringToByteArray(string str) {
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            return encoding.GetBytes(str);
        }

        public static bool WroteByteArrayToFileSuccessfully(byte[] buff, string fileName) {
            bool response = false;

            try {
                FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(buff);
                bw.Close();
                response = true;
            }
            catch (Exception ex) {
                Debug.WriteLine("");
                Debug.WriteLine("Error in WroteByteArrayToFileSuccessfully()");
                Debug.WriteLine(ex.Message);
                Debug.WriteLine("");
            }

            return response;
        }
    }
}

Initial URL


Initial Description
Some static methods to help in working with byte arrays

Initial Title
Working with byte arrays

Initial Tags


Initial Language
C#