Wednesday, May 13, 2009

C# Convert string to an array of bytes


Scenario
How to convert a string or a string array into a double array?
The .NET does not provide any method for us to call directly(if you found it, please let me know :D). However this is easy to implement within just a couple lines of code.

Method
//Convert 1 string to an array of bytes
public static byte[] ConvertToBytes(string str)
{
byte[] ans = new byte[str.Length];

for (int i = 0; i < ans.Length; i++)
{
ans[i] = Convert.ToByte(str[i]);
}

return ans;
}

comments

0 Responses to "C# Convert string to an array of bytes"

Post a Comment

BlogUpp!

 

Copyright 2009 All Rights Reserved Revolution Two Lifestyle theme by Brian Gardner | Blogger template converted & enhanced by eBlog Templates