title: Signed to Unsigned and Vice Versa (arithmetical)
body: <p>How to convert a number from unsigned to signed?</p>
  
  <p>signed: -32768 to 32767
  unsigned: 0 to 65535</p>
  
  <p>I am solving the problem in JavaScript. The situation is that I have a number that goes e.g. from 0 to 65535 and I want to convert it to a reasonable signed value.</p>
  
  <p>e.g.: 65535 should become -1.</p>
  
  <p>Please do not use any bit related operations but something arithmetical.</p>
  
  <p>I guess this should be language independent assuming that we use a data type that is big enough.</p>
  
  <p>Update:
  Implementation according to the answer further down:</p>
  
  <pre><code>function convertWordToShort(ival) {
      if (isNaN(ival) === false) {
          if (ival &gt; 32767) {
              ival = ival - 65536;
          }
      }
      return ival;
  }
  function convertShortToWord(ival) {
      if (isNaN(ival) === false) {
          if (ival &lt; 0) {
              ival = ival + 65536;
          }
      }
      return ival;
  }
  function convertIntToDWord(ival) {
      if (isNaN(ival) === false) {
          if (ival &lt; 0) {
              ival = ival + 4294967296;
          }
      }
      return ival;
  }
  function convertDWordToInt(ival) {
      if (isNaN(ival) === false) {
          if (ival &gt; 2147483647) {
              ival = ival - 4294967296;
          }
      }
      return ival;
  }
  </code></pre>