spu*_*ity 3 java for-loop mac-address
尝试使用以下方法创建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();
}
}
Run Code Online (Sandbox Code Playgroud)
其中print()是:
System.out.println(macAddr+strings[3]+strings[2]+":"+strings[1]+strings[0]);
Run Code Online (Sandbox Code Playgroud)
但我正在跑步:
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
等等
干杯:)
使用a 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++));
}
Run Code Online (Sandbox Code Playgroud)
示例输出:
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
Run Code Online (Sandbox Code Playgroud)
试试这个以保持简单:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出(缩写):
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4693 次 |
| 最近记录: |