// C# to convert a string to a char array.
static public char[] convertStringToCharArray(string str)
{
return str.ToCharArray();
}
// C# to convert a string to a byte array.
static public byte[] convertStringToByteArray(string str)
{
byte[] arrByte = new byte[str.ToCharArray().Length];
int i = 0;
foreach (char ch in str.ToCharArray())
{
arrByte[i] = (byte)ch;
i++;
}
return arrByte;
}
// C# to convert a string to a byte array.
public static byte[] convertStringToByteArray1(string str)
{
//http://geekswithblogs.net/waynerds/archive/2006/01/16/66056.aspx
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
// C# to convert a string to a byte array.
public static byte[] convertStringToByteArray2(string str)
{
//http://pankajkm.blogspot.com/2007/02/convert-string-to-byte-array-and-vice.html
return (new System.Text.UnicodeEncoding()).GetBytes(str);
}
// C# to convert a char array to a byte array.
static public byte[] convertCharArrayToByteArray(char[] ca)
{
byte[] arrByte = new byte[ca.Length];
int i = 0;
foreach (char ch in ca)
{
arrByte[i] = (byte)ch;
i++;
}
return arrByte;
}
// C# to convert a char array to a string.
static public string convertCharArrayToString(char[] ca)
{
string str;
str = ca.ToString(); //Or
str = new string(ca);
return str;
}
// C# to convert a byte array to a char array.
static public char[] convertByteArrayToCharArray(byte[] by)
{
//byte[] by = { 0x01, 0x02, 0x03 };
char[] chararray = new char[by.Length];
Array.Copy(by, chararray, by.Length);
return chararray;
}
// C# Convert a byte array to a string
public static string ConvertByteArrayToString(byte[] byteArray)
{
//http://geekswithblogs.net/waynerds/archive/2006/01/16/66056.aspx
byte[] dBytes = byteArray;
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
string str = enc.GetString(dBytes);
return str;
}
// C# Convert a byte array to a string
public static string ConvertByteArrayToString1(byte[] byteArray)
{
//http://pankajkm.blogspot.com/2007/02/convert-string-to-byte-array-and-vice.html
return (new System.Text.UnicodeEncoding()).GetString(byteArray);
}
// C# Convert a byte array to a string
public static string ConvertByteArrayToString2()
{
System.Net.WebClient webdata = new System.Net.WebClient();
const string sampleurl = "http://www.sampleurl.com/default.htm";
//the below statement assigns byte[]
byte[] byteArray = webdata.DownloadData(sampleurl);
//the below code converts byte[] type string type
string txt = System.Text.Encoding.GetEncoding("utf-8").GetString(byteArray);
return txt;
}
Thursday, July 23, 2009
Char to String to Byte Conversion
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment