User rcix - SnippetGoodmost recent 30 from http://snippetgood.com2010-09-07T04:50:48Zhttp://snippetgood.com/feeds/user/37http://www.creativecommons.org/licenses/by-nc/2.5/rdfhttp://snippetgood.com/questions/104/code-to-convert-from-8-bit-integers-to-16-bit-integers-and-vice-versaCode to convert from 8-bit integers to 16 bit integers and vice versa?RCIX2009-10-16T10:14:29Z2009-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#105Answer by RCIX for Code to convert from 8-bit integers to 16 bit integers and vice versa?RCIX2009-10-16T10:15:35Z2009-10-16T10:15:35Z<p>Here's at least one that ought do do nicely:</p>
<pre><code>/// <summary>
/// converts a short into two component bytes.
/// </summary>
/// <param name="input">the short to break down.</param>
/// <param name="high">the high byte output of the function.</param>
/// <param name="low">the low byte output of the function.</param>
public static void FromShort(short input, out byte high, out byte low)
{
high = (byte)(input >> 8);
low = (byte)(input - ((input >> 8) << 8));
}
/// <summary>
/// converts 2 component bytes into a short.
/// </summary>
/// <param name="high">the high byte of a short.</param>
/// <param name="low">the low byte of a short.</param>
/// <returns>the short that the two bytes represented.</returns>
public static short ToShort(byte high, byte low)
{
return (short)((short)low + ((short)high << 8));
}
</code></pre>
http://snippetgood.com/questions/89/if-statements-and-ranges/103#103Answer by RCIX for If statements and ranges RCIX2009-10-16T10:09:11Z2009-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 < min) { return min; }
if (value > 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>