User rcix - SnippetGood most recent 30 from http://snippetgood.com 2010-09-07T04:50:48Z http://snippetgood.com/feeds/user/37 http://www.creativecommons.org/licenses/by-nc/2.5/rdf http://snippetgood.com/questions/104/code-to-convert-from-8-bit-integers-to-16-bit-integers-and-vice-versa Code to convert from 8-bit integers to 16 bit integers and vice versa? RCIX 2009-10-16T10:14:29Z 2009-10-17T16:02:16Z <p>I'm looking for a simple pair of functions that will convert a pair of 8-bit integers into a single 16-bit integer type and vice versa. Does anyone know of one for .NET? </p> <p>[someone with more rep: please add the "c#" tag?]</p> http://snippetgood.com/questions/104/code-to-convert-from-8-bit-integers-to-16-bit-integers-and-vice-versa/105#105 Answer by RCIX for Code to convert from 8-bit integers to 16 bit integers and vice versa? RCIX 2009-10-16T10:15:35Z 2009-10-16T10:15:35Z <p>Here's at least one that ought do do nicely:</p> <pre><code>/// &lt;summary&gt; /// converts a short into two component bytes. /// &lt;/summary&gt; /// &lt;param name="input"&gt;the short to break down.&lt;/param&gt; /// &lt;param name="high"&gt;the high byte output of the function.&lt;/param&gt; /// &lt;param name="low"&gt;the low byte output of the function.&lt;/param&gt; public static void FromShort(short input, out byte high, out byte low) { high = (byte)(input &gt;&gt; 8); low = (byte)(input - ((input &gt;&gt; 8) &lt;&lt; 8)); } /// &lt;summary&gt; /// converts 2 component bytes into a short. /// &lt;/summary&gt; /// &lt;param name="high"&gt;the high byte of a short.&lt;/param&gt; /// &lt;param name="low"&gt;the low byte of a short.&lt;/param&gt; /// &lt;returns&gt;the short that the two bytes represented.&lt;/returns&gt; public static short ToShort(byte high, byte low) { return (short)((short)low + ((short)high &lt;&lt; 8)); } </code></pre> http://snippetgood.com/questions/89/if-statements-and-ranges/103#103 Answer by RCIX for If statements and ranges RCIX 2009-10-16T10:09:11Z 2009-10-16T10:09:11Z <p>Assuming jQuery syntax is similar to c#, so domething like this:</p> <pre><code>static int clampToRange(int value, int min, int max) { if (value &lt; min) { return min; } if (value &gt; max) { return max; } return value; } </code></pre> <p>Then all you need to do is this in your code:</p> <pre><code>level = clampToRange(level, 0, maxLevel); </code></pre>