DevKB
Web Development Knowledge Base
HOME | TOP 20 | WRITE AN ARTICLE |
Sections :



RSS RSS Feed

You too, please publish your useful code snippets in any programming language :
write an article !


Valid HTML 4.0 Transitional

Plateforme d'envoi de gros fichiers en ligne
Dépannage site web
Blog infogérance
Hébergement e-mail

Olivier Ligny - - 11/03/2008 - vue 9991 fois

hex2dec() / dec2hex() JavaScript functions

hex2dec(n) : convert from hexadecimal (ff) to decimal (255)

dec2hex(n) : convert from decimal (255) to hexadecimal (ff)

 

 

        function Hex2DecF(HexVal){
            hexChars = '0123456789ABCDEF';
            HexVal = HexVal.toUpperCase();
            DecVal = 0;
            temp = HexVal.substring(0,1);
            DecVal = (hexChars.indexOf(temp) * 16);
            temp = HexVal.substring(1);
            DecVal += hexChars.indexOf(temp);
            return DecVal;
        }

        function hex2dec(str) {
            r = 0;
            n = str.length/2;
            for(i=0; i<n; i++) {
                x = n-1-i;
                c = Hex2DecF(str.substring(i*2,i*2+2))*Math.pow(256,x);
                r += c;
            }
            return r;
        }

        function dec2hex(i) {
            a2 = '';
            ihex = hexQuot(i);
            idiff = i-(ihex*16);
            a2 = itohex(idiff) + a2;
            while( ihex >= 16) {
                itmp = hexQuot(ihex);
                idiff = ihex-(itmp*16);
                a2 = itohex(idiff) + a2;
                ihex = itmp;
            }
            a1 = itohex(ihex);
            res = a1 + a2+'';
            while(res.length%2!=0) res = '0'+res;
            return res ;
        }
        function hexQuot(i) {
            return Math.floor(i/16);
        }
        function itohex(m) {hexChars = '0123456789ABCDEF';return hexChars.charAt(m);}

 


SAM - 14/03/2008
Voici les miennes (enfin ... celles que j'ai pompées) :

HEX_to_DECI(n) : convertit depuis hexadecimal (ff) vers decimal (255)

DECI_to_HEXn) : convertit de decimal (255) en hexadecimal (ff)

function DECI_to_HEX ( num ) {  // num maxi = 255
  var x = '';
  x += "0123456789ABCDEF".charAt((num-num%16)/16)
      + "0123456789ABCDEF".charAt(num%16);
  return x;
  }

 function HEX_to_DECI ( num ) {  // valeur hexa ( A0 ou ff ou ...)
  var result = 0;
  var max = num.toString().length-1;
  for (var i=max; i>=0; i--) 
       {
       var digit = num.charAt(i).toUpperCase();
       switch(digit)
          {
          case "A": digit = 10; break;
          case "B": digit = 11; break;
          case "C": digit = 12; break;
          case "D": digit = 13; break;
          case "E": digit = 14; break;
          case "F": digit = 15; break;
          }
      result += digit * Math.pow(16, +max-i);
      }
  return result;
}


exemples :
- 165 -> A5 : javascript:alert(convert_DECI_to_HEX(165))
- A5 -> 165 : javascript:alert(convert_HEX_to_DECI('a5'))
SAM - 14/03/2008
Mais qu'est-ce que c'est que de m'envoyer mes liens-JS en blank ?

Ha! Ha! j'ai réussi à plonker l'truc là avec mon 2ième lien ;-)


Write a comment :
Your name :     E-mail (optional) :

AntiSpam : what animal is visible on this picture ? :

Nos partenaires : iPhone 8 Cases & Protection