I have class representing car with two attributes. I want to convert it to byte[] and save it to binary file.
class Car
{
private string type;
private int id;
public Car(string paType, int paId)
{
type = paType;
id = paId;
}
public byte[] ByteConverter()
{
byte[] bArr = new byte[14];
byte[] hlpType = Encoding.UTF8.GetBytes(this.type);
byte[] hlpId = BitConverter.GetBytes(this.id);
hlpType.CopyTo(bArr, 0);
hlpId.CopyTo(bArr, hlpType.Length);
return bArr;
}
}
Save method body:
Car c = new Car("abcd", 12);
FileStream fsStream = new FileStream("cardata.bin", FileMode.Create);
fsStream.Write(c.ByteConverter(), 0, 14);
fsStream.Close();
If I open file cardata.dat, there is string.
abcd
How to solve this? Thanks.