• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

String to Byte method

Hi all

I have this myReadFile Method that takes a path and outPut a byte array..

How can I modify the same method to take in String character

Since I this function in the message body that return a string value
string parseDocResult = "";
StreamReader reader = new StreamReader((Stream)msgInvoiceDoc[0].RetrieveAs(typeof(Stream)));
parseDocResult = reader.ReadToEnd();

Hence I will call
 myReadFile(parseDocResult);

Thereby eliminating the part
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

Thanks for your feeback

Regards


public static byte[] myReadFile(string filePath)
		{
			byte[] buffer;
			FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

			try
			{
				int length = (int)fileStream.Length;  // get file length
				buffer = new byte[length];            // create buffer
				int count;                            // actual number of bytes read
				int sum = 0;                          // total number of bytes read

				// read until Read method returns 0 (end of the stream has been reached)
				while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
					sum += count;  // sum is a buffer offset for next reading
			}
			finally
			{
				fileStream.Close();
			}
			return buffer;
		}

Open in new window

0
ZURINET
Asked:
ZURINET
1 Solution
 
nishant joshiTechnology Development ConsultantCommented:
public string myReadFile(string filePath)
        {
            byte[] buffer;
            System.IO.FileStream fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

            try
            {
                int length = (int)fileStream.Length;  // get file length
                buffer = new byte[length];            // create buffer
                int count;                            // actual number of bytes read
                int sum = 0;                          // total number of bytes read

                // read until Read method returns 0 (end of the stream has been reached)
                while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
                    sum += count;  // sum is a buffer offset for next reading
            }
            finally
            {
                fileStream.Close();
            }

            return System.Text.ASCIIEncoding.ASCII.GetString(buffer);
        }
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now