ushort to byte[]
Started by wjousts, Sep 06 2011 06:51 PM
5 replies to this topic
#1
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
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
Posted 06 September 2011 - 07:15 PM
#4
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
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
Posted 07 September 2011 - 04:01 AM
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.Interesting. I can probably use some of that. I'm just a little nervous about the unsafe code not being official supported.
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