using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
StringToByte
{
class Program
{
static void Main(string[]
args)
{
string
str="Kannadasan";
byte[]
bytes = new byte[str.Length
* sizeof(char)];
bytes =
GetByteArrayFromString(str);
Console.WriteLine(bytes.ToString());
string
output = "";
output =
GetStringFromByteArray(bytes);
Console.WriteLine(output.ToString());
Console.ReadLine();
}
// Static
method to convert string into char array then to Byte array
static byte[] GetByteArrayFromString(string InputString)
{
byte[]
bytes = new byte[InputString.Length
* sizeof(char)];
try
{
System.Buffer.BlockCopy(InputString.ToCharArray(),
0, bytes, 0, bytes.Length);
}
catch(Exception ex)
{
//Exception
Handling Code for GetByteArrayFromString
}
return
bytes;
}
// Static
method to convert Byte array to char array
static string GetStringFromByteArray(byte[] InputBytes)
{
char[]
chars = new char[InputBytes.Length
/ sizeof(char)];
try
{
System.Buffer.BlockCopy(InputBytes,
0, chars, 0, InputBytes.Length);
}
catch(Exception ex)
{
//Exception
Handling Code for GetStringFromByteArray
}
return
new string(chars);
}
}
}
Comments
Post a Comment