PDA

View Full Version : سوال: تبدیل Object به آرایه ای از بایت



mshirdelcs
شنبه 12 تیر 1389, 09:01 صبح
می خواستم یک Object که از DB خونده شده رو به آرایه ای از بایت تبریل کنم. مقداری که در Object ذخیره شده از نوع varbinary هست.
ممنون.:ناراحت:

mohammad meta
شنبه 12 تیر 1389, 09:49 صبح
سلام دوست عزیز
راه اول



byte[] bytData = (byte[])(your object)

راه دوم به این کار میگن serialization
using System.Runtime.Serialization.Formatters.Binary;


// 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;
}

mshirdelcs
یک شنبه 13 تیر 1389, 10:42 صبح
سلام .
خیلی ممنون آقای Mohammad Meta انشا الله از خدا هر چی بخوای بهت بده.
کار مارو راه انداختی.:لبخندساده: