0

I am trying to convert a string macaddress to hex value. I got a C# code which does the same thing but when I use the java code it gives me negative values. as in the C# code returns signed integer but how do i do the same in Java

following is part of my code

Integer hex = Integer.parseInt(MacAddress.subString(0,2), 16 );
MacAddress[0] = hex.byteValue();

i get something like 148 on the c# code and java returns -148 how can I sort this out thanks

UPDATE

and I just realised that my c# code returns a value of 214 to "D6" a part of the macaddress and the java code returns -42 which is strange

Mr.Noob
  • 1,005
  • 3
  • 24
  • 58

5 Answers5

4
String MacAddress="D6";
Integer hex = Integer.parseInt(MacAddress.substring(0,2), 16 );
byte byteArray=(byte) hex.intValue();
System.out.println(hex+"|"+hex.byteValue()+"|"+byteArray[0]+"|"+(int)(byteArray[0]&(0xff)));        

byteValue() will give you your byte in signed format.You can store this in your byte array without any worries,just take care of conversion into unsigned value before using it.

also see unsigned byte from signed byte

If you dont want this conversion from signed to unsigned Make your MacAddress as char[]. char are by default considered as unsigned.

Community
  • 1
  • 1
faizan
  • 680
  • 1
  • 6
  • 15
  • 1
    yeah but I need it in a byte array thats the whole problem – Mr.Noob Sep 21 '12 at 09:59
  • 1
    The value in your 'hex.byteValue()' is actually the binary for 214 but when you are trying to print it will give you negative value for numbers above 128. – faizan Sep 21 '12 at 10:16
  • 1
    So what you can do is just store hex.byteValue() for each hex in your array. – faizan Sep 21 '12 at 10:17
  • 1
    yeah but when I was debugging without printing the byte array contained negative value for 214 – Mr.Noob Sep 21 '12 at 10:17
  • 1
    The problem is java considers that your byte has an signed value thats why it will show you negative of 214 – faizan Sep 21 '12 at 10:21
  • you dont worry about the negative value,its just that whenever you are using this byte value convert it to unsigned by ANDing with(0xff) – faizan Sep 21 '12 at 10:22
  • can you give me an example please? – Mr.Noob Sep 21 '12 at 11:00
  • Like if you convert int 200 to a byte it will give you a negative value when you print or debug.Because 200 in binary is 11001000 and in signed binary any number starting with 1 (left) is considered to be negative. – faizan Sep 21 '12 at 11:15
  • but it should not worry you because no matter it is being considered signed or unsigned your value 11001000 is being stores correctly in the array. – faizan Sep 21 '12 at 11:16
  • Can you vote up my answer so that i will be able to move this discussion to chat – faizan Sep 21 '12 at 11:24
  • so what you mean is even if its a negative value the data will stored correctly? even if its a negative value? – Mr.Noob Sep 21 '12 at 12:27
  • yes.data is stored in proper correct binary format in your byte array. – faizan Sep 21 '12 at 13:08
  • if you try printing value in byte for 200 it will give you binary as 11001000 only but the problem is just that the debugger or println() will consider it as signed and will print -ve value if it starts with 1. – faizan Sep 21 '12 at 13:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16960/discussion-between-faizan-and-mr-noob) – faizan Sep 21 '12 at 13:24
1

Since bytes are signed in Java, you should probably do hex.intValue() instead (or just rely on the auto-unboxing).

Alexander Pavlov
  • 31,598
  • 5
  • 67
  • 93
1

You can just access the "hex" variable directly

System.out.println(hex);

This is because Java automatically unboxes the Integer object to the primitive int. This is helpful in many cases particularly to do with datastructures.

Sam P
  • 453
  • 6
  • 19
0

This is a representation issue. The "correct" data is stored in the computer internally. The default behavior is for Java to display a byte represented as a signed decimal in the range -128 to 127. Since you are using the binary data stored in the computer, you should print out the hex or binary representation of that value. This will help avoid the confusion caused here.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

ip = InetAddress.getLocalHost(); System.out.println("Current IP address : " + ip.getHostAddress());

    NetworkInterface network = NetworkInterface.getByInetAddress(ip);

    byte[] mac = network.getHardwareAddress();

StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));        
        }
        System.out.println(sb.toString());

Copy from here : http://www.mkyong.com/java/how-to-get-mac-address-in-java/comment-page-1/#comment-139182

Az MaYo
  • 41
  • 1