Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert Mac Address to Integer

Im trying convert mac adress to integer:

Result is: 2321591092112814

It should be: 255771439995918

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading