title: signed short to byte in c++
body: <p>I'm trying to convert a HEX number into a short (2 Bytes) using C++
  everything is OK except for one thing... signed conversion from short to Byte (last test)</p>
  
  <p>i found this question and couldn't really benefit from it: <a href="https://stackoverflow.com/questions/1862453/portable-signed-unsigned-byte-cast-c">portable signed/unsigned byte cast,C++</a></p>
  
  <p><strong>here are my tests:</strong></p>
  
  <pre><code>// test 1 - positive B2Short (success)
  byte *b = new byte[2];
  b[0] = 0x10; //low byte
  b[1] = 0x00; //heigh byte
  
  signed short test = 0;
  test = ByteToShort(b);
  cout &lt;&lt; test &lt;&lt; endl;
  
  // test 2 - negative B2Short (success)
  b[0] = 0xF0; //low byte
  b[1] = 0xFF; //heigh byte
  
  test = 0;
  test = ByteToShort(b);
  cout &lt;&lt; test &lt;&lt; endl;
  
  // test 3 - positive Short2B (success)
  signed short n = 11;
  ShortToByte(n, b);
  test = ByteToShort(b);  // for display to see if it worked
  cout &lt;&lt; test &lt;&lt; endl;
  
  // test 4 - negative Short2B (FAIL!)
  n = -11;
  ShortToByte(n, b);
  test = ByteToShort(b);  // for display to see if it worked
  cout &lt;&lt; test &lt;&lt; endl;
  </code></pre>
  
  <p><strong>functions used:</strong></p>
  
  <pre><code>signed short ByteToShort(byte* bytes){
  
      signed short result = 0;
      result = (result&lt;&lt;8) + bytes[1]; // heigh byte
      result = (result&lt;&lt;8) + bytes[0]; // low byte
      return result;
  }
  
  void ShortToByte(signed short num, byte* bytes){
  
      bytes[1] = num &amp; 0xFF00; // heigh byte
      bytes[0] = num &amp; 0x00FF; // low byte
  }
  </code></pre>
  
  <p><strong>outputs:</strong></p>
  
  <pre><code>16
  -16
  11
  245
  </code></pre>