csharp class to byte array
Code Example - csharp class to byte array
// Convert an object to a byte array
private byte[] ObjectToByteArray(Object obj)
{
if(obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
// Convert a byte array to an Object
private Object ByteArrayToObject(byte[] arrBytes)
{
MemoryStream memStream = new MemoryStream();
BinaryFormatter binForm = new BinaryFormatter();
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
Object obj = (Object) binForm.Deserialize(memStream);
return obj;
}
string from byte array csharp
var str = System.Text.Encoding.Default.GetString(result);
csharp string to byte array
string author = "Mahesh Chand";
// Convert a C# string to a byte array
byte[] bytes = Encoding.ASCII.GetBytes(author);
// Convert a byte array to a C# string.
string str = Encoding.ASCII.GetString(bytes);
csharp 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;
}
csharp get binary array from int
byte[] bytes = BitConverter.GetBytes(i);
String to byte array csharp
string string = "Hello";
byte[] bytes = Encoding.ASCII.GetBytes(string);
csharp int to byte Array
int number;
byte[] bytes = BitConverter.GetBytes(number);
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);