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

ushort to byte[]


  • Please log in to reply
5 replies to this topic

#1 wjousts

wjousts

    Member

  • Members
  • PipPip
  • 22 posts

Posted 06 September 2011 - 06:51 PM

Anybody know how to do the above? Convert a 16-bit integer to two bytes in the micro framework (and vice versa). In the full framework I'd just use BitConverter, but apparently that doesn't exist in the micro world. Also, I'm gonna need to convert 32-bit doubles to and from byte arrays.

#2 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 06 September 2011 - 07:05 PM

Convert a 16-bit integer to two bytes in the micro framework (and vice versa).

ushort x = 100;
var bytes = new byte[2];
// ushort -> byte[2]
Utility.InsertValueIntoArray(bytes, 0, 2, x);
// byte[2] -> ushort
x = (ushort)Utility.ExtractValueFromArray(bytes, 0, 2);
Not sure about the double, though, you'd probably need to use intermediate string and double.Parse() or Convert.ToDouble().

#3 Cabadam

Cabadam

    Advanced Member

  • Members
  • PipPipPip
  • 90 posts

Posted 06 September 2011 - 07:15 PM

Also, haven't used it, but could try taking a look at:
http://forums.netdui...8-bitconverter/

#4 wjousts

wjousts

    Member

  • Members
  • PipPip
  • 22 posts

Posted 06 September 2011 - 07:31 PM

ushort x = 100;
var bytes = new byte[2];
// ushort -> byte[2]
Utility.InsertValueIntoArray(bytes, 0, 2, x);
// byte[2] -> ushort
x = (ushort)Utility.ExtractValueFromArray(bytes, 0, 2);
Not sure about the double, though, you'd probably need to use intermediate string and double.Parse() or Convert.ToDouble().


InsertValueIntoArray works, but it's the wrong endian for what I need. But I can fix that fairly easily. For double, parsing to a string won't work. I need the IEEE-754 representation.

#5 wjousts

wjousts

    Member

  • Members
  • PipPip
  • 22 posts

Posted 06 September 2011 - 07:34 PM

Also, haven't used it, but could try taking a look at:
http://forums.netdui...8-bitconverter/


Interesting. I can probably use some of that. I'm just a little nervous about the unsafe code not being official supported.

#6 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 07 September 2011 - 04:01 AM

Interesting. I can probably use some of that. I'm just a little nervous about the unsafe code not being official supported.

I confirm that the byte composition is the best way, even on a PC. Much faster than other solutions, especially when there are few bytes involved.

Just another word...
The following function (from 8-bitconverter) is working:

public static byte[] GetBytes(ushort value)
        {
            return new byte[2] { (byte)(value & 0xFF), (byte)((value >> 8) & 0xFF) };
        }
...but it can be written better:

public static byte[] GetBytes(ushort value)
        {
            return new byte[2] { (byte)value, (byte)(value >> 8) };
        }
...so it takes two ANDs lesser, and the result is exactly the same.

About the unsafe code.
There are many people surprised by the few performance of the unsafe code (i.e. pointers). Many of you are expecting huge improvements by using pointers in C#, but most of the times they are slightly slower than an equivalent "safe" pattern.
However, the unsafe way to convert a double to bytes and vice-versa, is very handful.
Instead crying for the missing support for the unsafe code, I would cry for the missing support for several low-level utilities (like marshalling data, buffers handling, etc).
Cheers
Biggest fault of Netduino? It runs by electricity.




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.