Java for循环生成MAC地址

发布于 2024-12-06 04:17:30 字数 989 浏览 0 评论 0原文

尝试使用以下命令创建 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

等等

干杯:)

Trying to make a loop of MAC address values with:

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

where print() is:

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

But i'm getting run-over:

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

The two problems are the dual values at each crossover (e.g. AA:BB:CC:DD:00:0F)
and the values stopping at F on each value.

I'm trying to get them as:

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

etc.

Cheers :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤城病女 2024-12-13 04:17:30

使用 long 存储您的 Mac 地址并创建一个小函数将其转换为 String

public static String toMacString(long mac) {

    if (mac > 0xFFFFFFFFFFFFL || mac < 0)
        throw new IllegalArgumentException("mac out of range");

    StringBuffer m = new StringBuffer(Long.toString(mac, 16));
    while (m.length() < 12) m.insert(0, "0");

    for (int j = m.length() - 2; j >= 2; j-=2)
        m.insert(j, ":");
    return m.toString().toUpperCase();
}

public static void main(String[] args) {

    long mac = 0xAABBCCDD0000L;

    for (long i = 0; i < 1000; i++)
        System.out.println(toMacString(mac++));
}

输出示例:

AA:BB:CC:DD:00:00
AA:BB:CC:DD:00:01
AA:BB:CC:DD:00:02
AA:BB:CC:DD:00:03
AA:BB:CC:DD:00:04
AA:BB:CC:DD:00:05
AA:BB:CC:DD:00:06
....
AA:BB:CC:DD:03:DF
AA:BB:CC:DD:03:E0
AA:BB:CC:DD:03:E1
AA:BB:CC:DD:03:E2
AA:BB:CC:DD:03:E3
AA:BB:CC:DD:03:E4
AA:BB:CC:DD:03:E5
AA:BB:CC:DD:03:E6
AA:BB:CC:DD:03:E7

Use a long to store your mac address and create a small function to convert it to a String.

public static String toMacString(long mac) {

    if (mac > 0xFFFFFFFFFFFFL || mac < 0)
        throw new IllegalArgumentException("mac out of range");

    StringBuffer m = new StringBuffer(Long.toString(mac, 16));
    while (m.length() < 12) m.insert(0, "0");

    for (int j = m.length() - 2; j >= 2; j-=2)
        m.insert(j, ":");
    return m.toString().toUpperCase();
}

public static void main(String[] args) {

    long mac = 0xAABBCCDD0000L;

    for (long i = 0; i < 1000; i++)
        System.out.println(toMacString(mac++));
}

Example output:

AA:BB:CC:DD:00:00
AA:BB:CC:DD:00:01
AA:BB:CC:DD:00:02
AA:BB:CC:DD:00:03
AA:BB:CC:DD:00:04
AA:BB:CC:DD:00:05
AA:BB:CC:DD:00:06
....
AA:BB:CC:DD:03:DF
AA:BB:CC:DD:03:E0
AA:BB:CC:DD:03:E1
AA:BB:CC:DD:03:E2
AA:BB:CC:DD:03:E3
AA:BB:CC:DD:03:E4
AA:BB:CC:DD:03:E5
AA:BB:CC:DD:03:E6
AA:BB:CC:DD:03:E7
征﹌骨岁月お 2024-12-13 04:17:30

尝试这个以保持简单:

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

Try this for keeping it simple:

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

Output (abbreviated):

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
远昼 2024-12-13 04:17:30
    String[] Mac = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
    Random rd = new Random();
    rd.nextInt(15);
    String result="";

    for(int i=0;i<6;i++){
        String a = Mac[rd.nextInt(15)];
        String b = Mac[rd.nextInt(15)];
        result+=a+b;
        if(i<5){
            result+=":";
        }

    }
    String[] Mac = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
    Random rd = new Random();
    rd.nextInt(15);
    String result="";

    for(int i=0;i<6;i++){
        String a = Mac[rd.nextInt(15)];
        String b = Mac[rd.nextInt(15)];
        result+=a+b;
        if(i<5){
            result+=":";
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文