如何将十六进制转换为ip javascript?

时间:2011-08-03 06:55:10

标签: javascript

我需要在javascript中将十六进制转换为等效的IP吗? 您的十六进制值为0A.0A.0A.0A
我必须把它转换成ip?
10.10.10.10

请帮帮我

4 个答案:

答案 0 :(得分:4)

var 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(".");

答案 1 :(得分:1)

您可以使用splitparseIntjoin完成大部分工作:

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"

答案 2 :(得分:1)

'0A.0A.0A.0A'.replace( /(\w{2})/gi, function( str, match ) {
    return parseInt( match, 16 );
});

答案 3 :(得分:0)

我的两分钱。

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);