admin管理员组

文章数量:1431726

I'm trying to do some string manipulation. Is there a way to make this without a ton of if statements.

For a given MAC address I need to change the last value to +1. For example:

84:1D:A2:B9:A3:D0 => Needs to change to: 84:1D:A2:B9:A3:D1
84:1D:A2:B9:A3:99 => Needs to change to: 84:1D:A2:B9:A3:A0
84:1D:A2:B9:A3:AF => Needs to change to: 84:1D:A2:B9:A3:B0

var _cnames = "84:8D:C7:BB:A3:F0";
var res = _cnames.slice(15, 17);
if(res[1] == "0" || res[1] == "1" || res[1] == "2" || res[1] == "3"
|| res[1] == "4" || res[1] == "5" || res[1] == "6" || res[1] == "7"
|| res[1] == "8" || res[1] == "A" || res[1] == "B" || res[1] == "C"
|| res[1] == "D" || res[1] == "E"){
    res = res[0] + String.fromCharCode(res.charCodeAt(1) + 1);
}
if(res[1] == "F"){
    if(res[0] == "9"){
    res = "A0";
  }else{
        res = String.fromCharCode(res.charCodeAt(0) + 1) + "0";
  }
}
if(res[1] == "9"){
    res = res[0] + "A";
}
console.log(res);

This is my current solution but it's not as efficient as I'd like it to be. There has to be a better way to solve something like this.

I'm trying to do some string manipulation. Is there a way to make this without a ton of if statements.

For a given MAC address I need to change the last value to +1. For example:

84:1D:A2:B9:A3:D0 => Needs to change to: 84:1D:A2:B9:A3:D1
84:1D:A2:B9:A3:99 => Needs to change to: 84:1D:A2:B9:A3:A0
84:1D:A2:B9:A3:AF => Needs to change to: 84:1D:A2:B9:A3:B0

var _cnames = "84:8D:C7:BB:A3:F0";
var res = _cnames.slice(15, 17);
if(res[1] == "0" || res[1] == "1" || res[1] == "2" || res[1] == "3"
|| res[1] == "4" || res[1] == "5" || res[1] == "6" || res[1] == "7"
|| res[1] == "8" || res[1] == "A" || res[1] == "B" || res[1] == "C"
|| res[1] == "D" || res[1] == "E"){
    res = res[0] + String.fromCharCode(res.charCodeAt(1) + 1);
}
if(res[1] == "F"){
    if(res[0] == "9"){
    res = "A0";
  }else{
        res = String.fromCharCode(res.charCodeAt(0) + 1) + "0";
  }
}
if(res[1] == "9"){
    res = res[0] + "A";
}
console.log(res);

This is my current solution but it's not as efficient as I'd like it to be. There has to be a better way to solve something like this.

Share Improve this question edited Dec 29, 2019 at 17:07 Dexygen 12.6k13 gold badges86 silver badges151 bronze badges asked Dec 28, 2019 at 22:12 WilliamWilliam 456 bronze badges 2
  • This is hexadecimal addition. Maybe take a look at this stackoverflow./questions/11023144/… – Dan Hunex Commented Dec 28, 2019 at 22:20
  • 5 What if the last one was FF ? – Gabriele Petrioli Commented Dec 28, 2019 at 22:23
Add a ment  | 

4 Answers 4

Reset to default 5

Math in non-base 10 in JS

There is a surprisingly easy answer to this. The function parseInt() accepts a second argument, the ining value's radix. For example

parseInt("F0", 16) // Tells us that this is a base 16 (hex) number.

This will let us convert to decimal. Now the toString() method for Number types also accepts a radix for the outgoing value. So if we put it together it looks like this.

(parseInt("F0", 16) + 1 ).toString(16) // returns f1

This takes the value F0, a hex number, converts it to decimal adds the 1, and returns back a hex string.

I hope this helps!

Edit

To answer more specifically to your question, the context of a mac address is 00-FF so this below will properly pad with a leading zero, and using modulus 'wrap' any numbers over FF back down to 00 and up again.

("0"+ ((parseInt("FF", 16) + 1) % 256 ).toString(16)).substr(-2)

Here is a solution (that does not handle the FF case)

It basically uses parseInt with a radix of 16 to convert to a number, and then toString with a radix of 16 to convert to hex string

var _cnames = "84:8D:C7:BB:A3:F0";

var parts = _cnames.split(':');
var last = parts.pop();
var incremented = (parseInt(last,16) + 1).toString(16).toUpperCase().padStart(2,'0');
parts.push(incremented);

var updated = parts.join(':');

console.log(updated);

plete solution

Nota:
be carefull about values < 8 ==> '04' +1 => '5',
and need to add a leading zero for correct MAC Address

function nextMACaddress( MACaddress )
{
  let hex = MACaddress.split(':').map(x=>parseInt(x,16))
  plusOne(hex.length-1)
  return hex.map(x=>('0'+x.toString(16).toUpperCase()).slice(-2)).join(':')

  function plusOne(p) {
    if (p<0) return
    if (hex[p]==255) { hex[p]=0; plusOne(p-1) }
    else             { hex[p]++               }
  }
} 


console.log('84:1D:A2:B9:A3:D0 =>', nextMACaddress('84:1D:A2:B9:A3:D0') ) // 84:1D:A2:B9:A3:D1
console.log('84:1D:A2:B9:A3:99 =>', nextMACaddress('84:1D:A2:B9:A3:99') ) // 84:1D:A2:B9:A3:9A
console.log('84:1D:A2:B9:A3:AF =>', nextMACaddress('84:1D:A2:B9:A3:AF') ) // 84:1D:A2:B9:A3:B0
console.log('84:1D:A2:B9:A3:03 =>', nextMACaddress('84:1D:A2:B9:A3:03') ) // 84:1D:A2:B9:A3:04
console.log('84:1D:A2:FF:FF:FF =>', nextMACaddress('84:1D:A2:FF:FF:FF') ) // 84:1D:A3:00:00:00
console.log('FF:FF:FF:FF:FF:FF =>', nextMACaddress('FF:FF:FF:FF:FF:FF') ) // 00:00:00:00:00:00

Hi I am slightly confused about your requirements:

  • "For a given MAC address I need to change the last value to +1"; so you mean the last "octet", the last two characters ing after the last colon? Not the very last character, nor, as seems to be in at least a couple of the answers given, more than the last octet?
  • "84:1D:A2:B9:A3:99 => Needs to change to: 84:1D:A2:B9:A3:A0" -- I think you may be incorrect here about hexadecimal math, adding one to "99" results in "9A" not "A0"
  • Finally if I am correct about the first point above, I'm assuming if the last octet is "99" you want to wrap back around to "01"

(Finally finally LOL I may be making an incorrect assumption that these macAddrs are in an array but it should be simple enough to extract the logic from the Array.map method that follows)

Presuming I am correct about the above here is my concise solution which however uses padStart from ES2017 and therefore isn't supported by IE -- note I've done away with the need for split by using (the simpler?) slice, which is supported by IE

const macAddrs = [
  '84:1D:A2:B9:A3:D0',
  '84:1D:A2:B9:A3:99',
  '84:1D:A2:B9:A3:AF',
  '84:1D:A2:B9:A3:FF'
];

const addrsIncr = macAddrs.map(addr => {
  let addrPrefix = addr.slice(0, 15); // everything up to and including last colon
  let modulusIncrLastOctet = (parseInt(addr.slice(15), 16) + 1) % 256; // converts FF to 01
  return addrPrefix + modulusIncrLastOctet.toString(16).padStart(2, 0).toUpperCase();
});

console.log(addrsIncr);

OUTPUT:

[ '84:1D:A2:B9:A3:D1',
  '84:1D:A2:B9:A3:9A',
  '84:1D:A2:B9:A3:B0',
  '84:1D:A2:B9:A3:00' ]

Also see: https://repl.it/@dexygen/macaddrincr

本文标签: javascriptIncrementing hexadecimal string from MAC AddressStack Overflow