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
解决方案
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.
If you dont want this conversion from signed to unsigned Make your MacAddress as char[].
char are by default considered as unsigned.