0

C++

unsigned char lbytes[] = { 0x0A, 0x01, 0x02 };
unsigned char bytes[10];

double dbl = sin((double)i) * 10; 

i+=5; if (i == 360) i = 0;

memcpy(&bytes, &lbytes, 3);
cout << dbl << endl;
memcpy(&bytes[3], &dbl, sizeof(double));

C#

switch (m[1]) {
    case 0x01: {
        if (m.Length > 5) {
            double myval = Math.Round( BitConverter.ToDouble(m, 3), 2 );

but something is going wrong here, myval is alsways some very small value and with rount it's always 0.

question is: where is my mistake, I can see valid value in cout, I send 10 bytes, what is going wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
cnd
  • 32,616
  • 62
  • 183
  • 313
  • 2
    Aren't you copying your `double` beyond the boundary of your array? – Tony The Lion Oct 30 '12 at 11:25
  • 1
    Sounds like an endianness difference (big-endian vs little-endian). `BitConverter` uses CPU-endianness, so you might have to reverse the chunk. I posted some answers on that recently... try: http://stackoverflow.com/questions/13044200/converting-float-values-of-big-endian-to-little-c-sharp/13044715#13044715 (you'll need to expand to 8 bytes for double, obviously) – Marc Gravell Oct 30 '12 at 11:25
  • that's just my fault of sending only 10 bytes – cnd Oct 30 '12 at 11:49

1 Answers1

1

The problem is that you send only ten bytes. A double is eight byte, plus your header of three bytes which means that you should send 11 bytes.

If you adjust your bytes array and send the correct length it should work.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621