Data Conversion?
-
I have these code: protected bool EEWrite(UInt16 Address, byte Size, object[] ByteData) { bool Status = true; object[] Data = new object[4 + Size]; Data[0] = SerialNumber; Data[1] = (byte)0x80; Data[2] = (UInt16)Address; Data[3] = (byte)Size; for(int i = 4; i<=4+Size;i++) { Data[i] = (byte)ByteData[i - 4]; } Status = Device.Write( Data); return Status; } Then i 'd like to write a float number write_number as below: Status = EEWrite(0x9021, 1, ByteData); How would i convert a float write_number into ByteData? Thanks !
-
I have these code: protected bool EEWrite(UInt16 Address, byte Size, object[] ByteData) { bool Status = true; object[] Data = new object[4 + Size]; Data[0] = SerialNumber; Data[1] = (byte)0x80; Data[2] = (UInt16)Address; Data[3] = (byte)Size; for(int i = 4; i<=4+Size;i++) { Data[i] = (byte)ByteData[i - 4]; } Status = Device.Write( Data); return Status; } Then i 'd like to write a float number write_number as below: Status = EEWrite(0x9021, 1, ByteData); How would i convert a float write_number into ByteData? Thanks !
-
object[] byteData = BitConverter.GetBytes(write_number);
I think that is what you want.
Life goes very fast. Tomorrow, today is already yesterday.
-
then assign in to a byte array and then if you need that as an object array loop through the byte array and pass each one to an object array.
byte[] ba = BitConverter.GetBytes(write_number);
object[] oa = new object[ba.Length];
for(int i = 0; i < oa.length; i++)
oa[i] = ba[i];Life goes very fast. Tomorrow, today is already yesterday.