编程语言
首页 > 编程语言> > Java for循环生成MAC地址

Java for循环生成MAC地址

作者:互联网

尝试使用以下方法创建MAC地址值循环:

String macAddr = "AA:BB:CC:DD:";
char[] chars = {'A', 'B', 'C', 'D', 'E', 'F'};
String[] strings = {"0", "0", "0", "0"};

for (int i=0; i<strings.length; i++)
{
    //counter from 0 to F
    for (int d = 0; d <= 9; d++)
    {
        strings[i] = ""+d;
        print();
    }
    for (int d = 0; d< chars.length; d++)
    {
        strings[i] = ""+chars[d];
        print();
    }
}

其中print()是:

System.out.println(macAddr+strings[3]+strings[2]+":"+strings[1]+strings[0]);

但我正在跑步:

AA:BB:CC:DD:00:0D
AA:BB:CC:DD:00:0E
AA:BB:CC:DD:00:0F
AA:BB:CC:DD:00:0F
AA:BB:CC:DD:00:1F
AA:BB:CC:DD:00:2F
AA:BB:CC:DD:00:3F

这两个问题是每次交叉时的双重值(例如AA:BB:CC:DD:00:0F)
以及每个值在F处停止的值.

我想把它们当作:

AA:BB:CC:DD:00:0D
AA:BB:CC:DD:00:0E
AA:BB:CC:DD:00:0F
AA:BB:CC:DD:00:11
AA:BB:CC:DD:00:12
AA:BB:CC:DD:00:13

等等

干杯:)

解决方法:

试着这个来保持简单:

public static void main(String... args) {
    String macAddr = "AA:BB:CC:DD:";
    for (int i = 0; i < 256; i++) {
        for (int j = 0; j < 256; j++) {
            String fullAddr = String.format(macAddr + "%02X:%02X", i, j);
            System.out.println(fullAddr);
        }
    }
}

输出(缩写):

AA:BB:CC:DD:00:00
AA:BB:CC:DD:00:01
AA:BB:CC:DD:00:..
AA:BB:CC:DD:00:10
AA:BB:CC:DD:00:0A
AA:BB:CC:DD:00:..
AA:BB:CC:DD:00:0F
AA:BB:CC:DD:00:10
AA:BB:CC:DD:00:..
AA:BB:CC:DD:00:FF
AA:BB:CC:DD:01:00
AA:BB:CC:DD:..:..
AA:BB:CC:DD:FF:FF

标签:java,for-loop,mac-address
来源: https://codeday.me/bug/20190721/1495176.html