Home > Software design >  Convert Mac Address to Integer
Convert Mac Address to Integer

Time:11-19

Im trying convert mac adress to integer:

Result is: 2321591092112814

It should be: 255771439995918

Im trying:


String[] macAddressParts = device.getAddress().split(":");

Byte[] macAddressBytes = new Byte[6];
String macAddressString = "";

for(int i=0; i<6; i  ){
   Integer hex = Integer.parseInt(macAddressParts[i], 16);
   macAddressString  = hex.toString();

}
System.out.println(macAddressString);

// 2321591092112814

CodePudding user response:

You cannot make an integer by catenating strings.

Try that :

String[] macAddressParts = "e8:9f:6d:d3:1c:0e".split(":");

Byte[] macAddressBytes = new Byte[6];
long addressAsInteger = 0;
for (int i = 0; i < 6; i  ) {
    Integer hex = Integer.parseInt(macAddressParts[i], 16);
    addressAsInteger = addressAsInteger * 256   hex;
}
System.out.println("Addresse as an integer : "   addressAsInteger);

It gives the right answer : 255771439995918.

  • Related