I need to convert a hexadecimal into equivalent IP in javascript?
My hexadecial value is
0A.0A.0A.0A
and i have to convert this into ip?
10.10.10.10
Please help me
I need to convert a hexadecimal into equivalent IP in javascript?
My hexadecial value is
0A.0A.0A.0A
and i have to convert this into ip?
10.10.10.10
Please help me
Share Improve this question asked Aug 3, 2011 at 6:55 DhineshDhinesh 111 silver badge4 bronze badges 1- Possible duplicate: stackoverflow./questions/57803/… – OverZealous Commented Aug 3, 2011 at 7:00
4 Answers
Reset to default 4var hex = "0A.0A.0A.0A";
var splitData = hex.split(".");
for (var i = 0; i < splitData.length; i++){
splitData[i] = parseInt(splitData[i], 16);
}
var ip = splitData.join(".");
'0A.0A.0A.0A'.replace( /(\w{2})/gi, function( str, match ) {
return parseInt( match, 16 );
});
You can use split
, parseInt
, and join
to do most of the work:
addr = '0A.0A.0A.0A';
// Break it into hex pieces.
parts = addr.split('.');
// Convert each piece to decimal using parseInt's radix argument.
for(var i = 0; i < parts.length; ++i)
parts[i] = parseInt(parts[i], 16);
// And put it back together using join and implicit conversion
// of numbers to strings.
addr = parts.join('.');
// addr is now "10.10.10.10"
My two cents.
function ConvertHexIPToBase10(ip) {
var vals = ip.split(".");
var op = [];
for (var i = 0; i < vals.length; i++) {
op.push(parseInt(vals[i], 16));
}
return op.join(".");
}
var hexIP = "0A.0A.0A.0A";
var newIP = ConvertHexIPToBase10(hexIP);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744750424a4591559.html
评论列表(0条)