Link to home
Start Free TrialLog in
Avatar of ZURINET
ZURINET

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of nishant joshi
nishant joshi
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial