Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

How do you get the bytes of a float?


  • Please log in to reply
5 replies to this topic

#1 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 17 December 2011 - 09:14 AM

Reading the posts I found that serialization is not implemented. The Bitconverter post asserted that you could run unsafe code despite the fact that all of the docs say that you cannot. I used this example as a model,

http://msdn.microsof...y/etb4xxec.aspx

and ran this code which absolutely clobbered my Netduino. Fortunately, I managed to restore it. I tried it a second time and this time it also crashed Visual Studio and my PC.
using System;
using Microsoft.SPOT;

namespace GetBytes_Netduino_Program
{
    public class Program
    {
        public static void Main()
        {
            byte[] theBytes = singleBytes.getSingleBytes(1234.55f);
        }
    }
    public class singleBytes
    {
        public static byte[] getSingleBytes(float number)
        {
            byte[] theBytes;
            unsafe
            {
                byte* p = (byte*)&number;
                theBytes = new byte[sizeof(float)];
                for (int i = 0; i < sizeof(float); ++i)
                {
                    theBytes[i] = *p;
                    p++;
                }
            }
            return theBytes;
        }
    }
}
If I run this as a windows console application the result is: [0] = 154, [1] = 81, [2] = 154, [3] = 68. Are there any other methods to accomplish this task on a Netduino Plus?
Baxter

#2 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 17 December 2011 - 12:54 PM

Hello Baxter.
There would be a better way, but it seems not working. The funniest thing is that I've tried some day ago, and I've found a kind of "bug" (?).
If you try the follows, on a regular .Net framework works fine. Strangely, on Netduino (MF?) it throws an Exception...why?



        public static int InsertValue(
            byte[] buffer,
            int position,
            Single value)
        {
            fixed (byte* temp = buffer)
            {
                *(Single*)(temp + position) = value;
            }

            return sizeof(Single);
        }

However, it seems that there's a problem on the "sizeof" the datum: the follows works fine for me...



        public static int InsertValue(
            byte[] buffer,
            int position,
            Boolean value)
        {
            fixed (byte* temp = buffer)
            {
                *(Boolean*)(temp + position) = value;
            }

            return sizeof(Boolean);
        }

IMHO, the binary serialization should be one of the most important things to implement/consider in such a tiny framework, where often you cannot use all the elegant patterns and libraries as a PC. Moving data from/to other devices is the fundamental goal of Netduino, thus exchanging floats, ints, and similar should be a very common task to achieve.
Cheers
Biggest fault of Netduino? It runs by electricity.

#3 Geancarlo

Geancarlo

    Member

  • Members
  • PipPip
  • 24 posts

Posted 17 December 2011 - 03:08 PM

If the data you want to serialize will remain in .netmf world, I believe you can use Reflection.Serialize/Deserialize

#4 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 17 December 2011 - 03:17 PM

Strangely, on Netduino (MF?) it throws an Exception...why?

What exception is thrown? The reason could be that pointers are not supported in the current implementation of .NET Micro Framework.

#5 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 17 December 2011 - 04:41 PM

Stanislav (CW2), I really don't know what there's behind the exception. I may understand that some exception could happen because certain features are not supported... I'm just wondering whether is it worthwhile missing the pointer support. Cheers

Attached Files


Biggest fault of Netduino? It runs by electricity.

#6 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 17 December 2011 - 04:59 PM

I really don't know what there's behind the exception.

I guess the CLR_E_WRONG_TYPE exception indicates the pointer type is not recognized/allowed there. If I remember it correctly the pointers are not officially supported, although they may partially work...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.