Search code examples
c#castingchardoublebyte-shifting

how to cast doubles from char array


I am shifting in 3 chars from serial port every 3rd char is a double that i split up for the port, so now i need to put it back to gether.

(Value before sending = 0x3F3400 or 0.703125)

char[0] = 0x3F  (msb)
char[1] = 0x34
char[2] = 0x00  (lsb)

double total = (char[0] << 16)+(char[1] << 8)+(char[2]); // this part did not work

Pls advise how to rebuild this, I dont understand doubles well enough, I cannot even tell which part of a double is the decimal point or - sign thanks!


Solution

  • I can't get yor value, but as help can advice you to use BitConverter and byte array like this:

    var v = new byte[8];
    v[7] = 0x3F;
    v[6] = 0x34;
    v[5] = 0x00;
    double total = BitConverter.ToDouble(v, 0);
    Console.WriteLine(total.ToString("0.0000000000000"));