Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert integer MAC address to string in Javascript

I have a MAC address stored as a raw 48 bit number, and I want to split it up and print it in the standard hex format of xx:xx:xx:xx:xx:xx. For example, the raw number 81952921372024 should become 78:45:c4:26:89:4a. My first attempt was,

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

But because Javascript apparently can't handle > 32 bit integers when doing shift operations, the last two octets always come out to 0,

78:45:c4:26:0:0 
like image 475
PherricOxide Avatar asked Jul 29 '13 20:07

PherricOxide


2 Answers

A simple approach looks as follows:

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"

However I suggest putting additional 00 groups just for pathological cases when your integer is very short (i.e. 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"
like image 166
oleq Avatar answered Sep 28 '22 07:09

oleq


The following does the job,

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(':'));

Output:
28:cf:e9:1e:c6:05

EDIT:

to take care of trailing 0's

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

Output:
00:45:d9:64:b8:00

like image 38
v2b Avatar answered Sep 28 '22 07:09

v2b