0
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);
但事情错在这里,设为myVal是alsways一些非常小的值,并与rount它始终为0。
问题是:我的错误在哪里,我可以在cout中看到有效值,我发送10个字节,出了什么问题?
难道你不是在拷贝数组的边界吗? –
听起来像一个排序差异(大端与小端)。 'BitConverter'使用CPU-endianness,所以你可能不得不倒转块。我最近发布了一些答案...试试:http://stackoverflow.com/questions/13044200/converting-float-values-of-big-endian-to-little-c-sharp/13044715#13044715(你'我需要扩大到8个字节的双倍,显然) –
这只是我的错误发送只有10个字节 – Cynede