在Javascript中将整数MAC地址转换为字符串

时间:2013-07-29 20:22:08

标签: javascript mac-address

我将MAC地址存储为原始的48位数字,我想将其拆分并以xx:xx:xx:xx:xx:xx的标准十六进制格式打印。例如,原始编号81952921372024应为78:45:c4:26:89:4a。我的第一次尝试是,

var suspect = {mac: 2333752735057272};
console.log(
    Number(suspect.mac & 0xFF).toString(16) + ":" +
    Number((suspect.mac & 0xFF00) >> 8).toString(16) + ":" +
    Number((suspect.mac & 0xFF0000) >> 16).toString(16) + ":" +
    Number((suspect.mac & 0xFF000000) >> 24).toString(16) + ":" +
    Number((suspect.mac & 0xFF00000000) >> 32).toString(16) + ":" +
    Number((suspect.mac & 0xFF0000000000) >> 48).toString(16));

但是因为Javascript显然无法处理>进行移位操作时,32位整数,最后两个八位字节总是为0,

78:45:c4:26:0:0 

3 个答案:

答案 0 :(得分:13)

一种简单的方法如下:

var mac = 81952921372024;

mac.toString( 16 )             // "4a8926c44578"
        .match( /.{1,2}/g )    // ["4a", "89", "26", "c4", "45", "78"]
        .reverse()             // ["78", "45", "c4", "26", "89", "4a"]
        .join( ':' )           // "78:45:c4:26:89:4a"

> "78:45:c4:26:89:4a"

但是,如果您的整数很短(即00),我建议仅针对病态情况添加其他mac = 150组:

var mac = 81952921372024;

new Array( 6 ).join( '00' )    // '000000000000'
    .match( /../g )            // [ '00', '00', '00', '00', '00', '00' ]
    .concat( 
        mac.toString( 16 )     // "4a8926c44578"
           .match( /.{1,2}/g ) // ["4a", "89", "26", "c4", "45", "78"]
    )                          // ["00", "00", "00", "00", "00", "00", "4a", "89", "26", "c4", "45", "78"]
    .reverse()                 // ["78", "45", "c4", "26", "89", "4a", "00", "00", "00", "00", "00", "00", ]
    .slice( 0, 6 )             // ["78", "45", "c4", "26", "89", "4a" ]
    .join( ':' )               // "78:45:c4:26:89:4a"

> "78:45:c4:26:89:4a"

答案 1 :(得分:1)

以下工作,

var hexValue = parseInt('44873434449413').toString(16);

var macaddress = [];

for (var i=0; i < hexValue.length; i=i+2) {
    macaddress.push(hexValue.substr(i,2));    
}


console.log(macaddress.join(':'));

输出:
28:cf:e9:1e:c6:05

编辑:

照顾尾随0的

str='3';
if (str.length < 12) { str = pad_after(str, 12, 0);}

var hexValue = parseInt(str).toString(16);

if (hexValue.length < 12) { hexValue = pad_before(hexValue, 12, 0);}

var macaddress = [];

for (var i=0; i < hexValue.length; i=i+2) {
    macaddress.push(hexValue.substr(i,2));    
}

console.log(macaddress.join(':'));


function pad_before(n, width, z) {
  z = z || '0';
  n = n + '';
  return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}

function pad_after(n, width, z) {
  z = z || '0';
  n = n + '';
  return n.length >= width ? n : n + new Array(width - n.length + 1).join(z);
}

输出:
00:45:d9:64:b8:00

答案 2 :(得分:0)

接受的答案没有通过我的单元测试(将mac地址转换为数字,然后转换回数字,并期望结果等于输入)。

@blueren表示接受的答案会返回相反的结果。

我的版本是:

(mac_num).toString(16).padStart(12, '0').match(/../g)
                .reverse()
                .slice(0, 6)
                .reverse()
                .join(':')

我的测试:

        expect(mac.toString(mac.toLong('b4:ae:2b:db:1c:3d'))).to.deep.equal('b4:ae:2b:db:1c:3d');
        expect(mac.toString(mac.toLong('00:00:00:db:1c:3d'))).to.deep.equal('00:00:00:db:1c:3d');
        expect(mac.toString(mac.toLong('b4:ae:2b:00:00:00'))).to.deep.equal('b4:ae:2b:00:00:00');
        expect(mac.toString(mac.toLong('00:00:00:00:00:00'))).to.deep.equal('00:00:00:00:00:00');
        expect(mac.toString(mac.toLong('10:00:00:00:00:00'))).to.deep.equal('10:00:00:00:00:00');
        expect(mac.toString(mac.toLong('00:00:01:00:00:00'))).to.deep.equal('00:00:01:00:00:00');
        expect(mac.toString(mac.toLong('00:00:00:00:00:01'))).to.deep.equal('00:00:00:00:00:01');