7

MacAddress を 16 進文字列に変換してから、Java でバイトに解析するにはどうすればよいですか? 同様にIPアドレスも?

ありがとうございました

4

2 に答える 2

18

MAC アドレスはすでに 16 進数形式になっており、2 桁の 16 進数が 6 組になった形式になっています。

String macAddress = "AA:BB:CC:DD:EE:FF";
String[] macAddressParts = macAddress.split(":");

// convert hex string to byte values
Byte[] macAddressBytes = new Byte[6];
for(int i=0; i<6; i++){
    Integer hex = Integer.parseInt(macAddressParts[i], 16);
    macAddressBytes[i] = hex.byteValue();
}

と...

String ipAddress = "192.168.1.1";
String[] ipAddressParts = ipAddress.split("\\.");

// convert int string to byte values
Byte[] ipAddressBytes = new Byte[4];
for(int i=0; i<4; i++){
    Integer integer = Integer.parseInt(ipAddressParts[i]);
    ipAddressBytes[i] = integer.byteValue();
}
于 2012-05-31T18:41:25.980 に答える