0

Code, give item is a valid ip (i.e. 254.253.242.222 )

     `var h0 = Math.pow(256,0);`
 `var h1 = Math.pow(256,1);`
    `var h2 = Math.pow(256,2);`

var h3 = Math.pow(256,3);

    var splitup = item.split('.');       var iHex =  (splitup[3] * h0) + (splitup[2] * h1) + (splitup[1]* h2) + (splitup[0] * h3) ;
    var hhexip = parseInt(iHex,16);
    $('#hexip').val($('#hexip').val() + "0x" + hhexip +"\n" );

this site http://www.silisoftware.com/tools/ipconverter.php?convert_from=254.253.242.222

edit

fixed code to use math.pow... still getting wrong out put though. says it should be 0xFEFDF2DE

but i get 0x285481457222

help?

Arian
  • 325
  • 1
  • 15
  • `^` is the bitwise xor operation in JavaScript. Try `256^3` and see what you get (answer: 259). – Matt Jan 15 '14 at 18:35
  • essentially a duplicate http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript – Charles380 Jan 15 '14 at 18:47

1 Answers1

0
var item = "254.253.242.222";
var splitup = item.split('.');
var iHex = parseInt(splitup[0]).toString(16) + parseInt(splitup[1]).toString(16) + parseInt(splitup[2]).toString(16) + parseInt(splitup[3]).toString(16);
var hhexip = parseInt(iHex,16);
$('#hexip').val($('#hexip').val() + "0x" + hhexip +"\n" );
jsight
  • 27,819
  • 25
  • 107
  • 140