C# bits to byte code example
Example 1: c# string to byte[]
using System.Text;
public static byte[] encode(string stringToEncode)
{
UTF8Encoding utf8 = new UtF8Encoding();
byte[] bytename = new byte[1024];
bytename = utf8.GetBytes(stringToEncode);
return bytename;
}
Example 2: c# number to byte
public byte[] ConvertNumToByte(int Number)
{
byte[] ByteArray = new byte[32];
string BinString = Convert.ToString(Number, 2);
char[] BinCharArray = BinString.ToCharArray();
try
{
System.Array.Reverse(BinCharArray);
if (BinCharArray != null && BinCharArray.Length > 0)
{
for (int index = 0; index < BinCharArray.Length; ++index)
{
ByteArray[index] = Convert.ToByte(Convert.ToString(BinCharArray[index]));
}
}
}
catch
{
}
return ByteArray;
}