title: Convert hexadecimal color to integer in javascript
body: <p>I'm trying to convert a hexadecimal color string to a int in javascript.</p>
  
  <p>The color int must be the same format as VB6. I think the bytes are not in the normal order. Ex: 255 is red (#ff0000) and 16776960 is Aqua (#00ffff)</p>
  
  <p>I have a function to do the inverse: (But someone in the comments told me that it's not correct)</p>
  
  <pre><code>function VBColorToHEX(i) {
      var hex = (i &amp; 0xFF).toString(16) +
                ((i &gt;&gt; 8) &amp; 0xFF).toString(16) +
              ((i &gt;&gt; 16) &amp; 0xFF).toString(16) +
              ((i &gt;&gt; 24) &amp; 0xFF).toString(16);
  
      hex += '000000';
      hex = hex.substring(0, 6);
      return "#" + hex;
  }
  </code></pre>
  
  <p>But was unable to write a function to return to my initial value.</p>
  
  <p>Can you help me?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I corrected my original function by padding each separate colors:</p>
  
  <pre><code>function VBColorToHEX(i) {
     var r = (i &amp; 0xFF).toString(16);
     var g = ((i &gt;&gt; 8) &amp; 0xFF).toString(16);
     var b = ((i &gt;&gt; 16) &amp; 0xFF).toString(16);
  
     r = ('0' + r).slice(-2);
     g = ('0' + g).slice(-2);
     b = ('0' + b).slice(-2);
  
     return "#" + r + g + b;
  }
  </code></pre>