Mac add and subtract, MAC address plus 1,mac address minus 1

Source: Internet
Author: User
Tags locale

Before I wrote a MAC address plus minus 1 conversion of the article, there are some less comprehensive, here I re-a blog to explain.

The real address of the Mac should be: ADCF4D5FD3CF, this, not AD:CF:4D:5F:D3:CF, this is the artificial colon, in order to look convenient;

How about the MAC address minus 1? In fact, since the MAC address is ADCF4D5FD3CF This format, then it is a 16 binary number, so convert it to 10 binary, and then add and subtract it on the line? Theoretically no problem, the average person is so to convert 16 binary to 10 binary:

int tenformat = Integer.parseint ("ADCF4D5FD3CF", 16);
Then we can have a try, this will certainly be wrong (java.lang.NumberFormatException), we see the MAC is 12 bits, then int can save a few?

Crossing, let's see what else we can save so much, well, you're right. Long, let's try it:

Long.tohexstring (Long.parselong (Mac, +) + add). toUpperCase (Locale.getdefault ());
Alas, yes, that's right, so the correct MAC address addition and subtraction should be this:

/** * Mac Add * @author YOLANDA * @param mac MAC address, eg:abcdef56bfd0 * @param add number to add * @return */public static String Getmac Add (String mac, int add) {return long.tohexstring (Long.parselong (Mac, +) + add). toUpperCase (Locale.getdefault ()); /** * Mac Subtraction * @author YOLANDA * @param mac MAC Address: Eg:abcdef56bfd0 * @param minus to reduce the number of * @return */public static String Getm Acminus (String mac, int minus) {return long.tohexstring (Long.parselong (MAC)-minus). toUpperCase (Locale.getdefault ());}

Here are a few things to note:

1, I met the general is the Mac plus minus 1 of the demand, so it is best to pass in the number of added and minus to make a judgment, to do the limit

2, there are some Mac conversion is only the last one plus minus 1, so how to calculate it?

In fact, the second kind is more complicated than the first one, how to say it? Because it cannot be converted directly. Here's the idea:

We first have to take out the last one, turn it into 10 decimal number, then add 1 to it, then convert it to 16 characters, and the previous 11 bits together, then if the last one is 0 or F? Is it a little complicated to think about it?

Then we first want to think about how the 16 binary is arranged, it contains letters, not the decimal 0123456789, but 0123456789ABCDEF such as 16 bits, we understand this point to know how to add and subtract, the following directly to provide the algorithm:

/** * Mac mantissa addition * @author YOLANDA * @param mac MAC Address: CAFDEFB469DF * @param add number to add * @return returns the MAC address after the add-minus */public static String Getmacadd (String mac, int add) {string Lastchar = Mac.substring (Mac.length ()-1). toUpperCase (Locale.getdefault () Mac = mac.substring (0, Mac.length ()-1), if ("F". Equals (Lastchar)) {Lastchar = "0";} else {int tempchar = Integer.parseint (Lastchar, +) + Add;lastchar = integer.tohexstring (Tempchar). toUpperCase (Locale.getdefault ());} Return (Mac + Lastchar);} /** * Mac mantissa Subtraction * @author YOLANDA * @param mac MAC Address: CAFDEFB469DF * @param minus to Subtract * @return returns the MAC address after the addition of */public Stat IC string Getmacminus (string mac, int minus) {string Lastchar = Mac.substring (Mac.length ()-1). toUpperCase ( Locale.getdefault ()); mac = mac.substring (0, Mac.length ()-1), if ("0". Equals (Lastchar)) {Lastchar = "F";} else {int Tempchar = Integer.parseint (Lastchar, +)-Minus;lastchar = Integer.tohexstring (Tempchar). toUpperCase ( Locale.getdefault ());} Return (Mac + Lastchar);}


Mac add and subtract, MAC address plus 1,mac address minus 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.