从I386上移植到PPC上,涉及到字节序的转换
发现原来的程序将网卡地址从buffer赋值给本地变量(代码里用一个64的长整形变量来记录mac地址ide )时没有进行字节序的转换,那么肯定是在显示时进行了转换,找到下面的代码:
public
static
String MacLongToString(
long
macAddr)
...
{ StringBuffer buf = new StringBuffer(); long l = (macAddr >> 32) & 0xff; if (l < 16) buf.append('0'); buf.append(Long.toHexString(l)); buf.append(':'); l = (macAddr >> 40) & 0xff; if (l < 16) buf.append('0'); buf.append(Long.toHexString(l)); buf.append(':'); l = macAddr & 0xff; if (l < 16) buf.append('0'); buf.append(Long.toHexString(l)); buf.append(':'); l = (macAddr >> 8) & 0xff; if (l < 16) buf.append('0'); buf.append(Long.toHexString(l)); buf.append(':'); l = (macAddr >> 16) & 0xff; if (l < 16) buf.append('0'); buf.append(Long.toHexString(l)); buf.append(':'); l = (macAddr >> 24) & 0xff; if (l < 16) buf.append('0'); buf.append(Long.toHexString(l)); return buf.toString(); }
一方面这个代码不够简洁,另外字节序的问题不应该在这里进行处理。所以我在从packet中取mac地址时就进行了字节序转换(一个原则就是把packet中的字段赋值给变量时一定要进行字节序的转换),所以在Java这边就不用关心字节序的问题,修改后的代码如下:
public
static
String MacLongToString(
long
macAddr)
...
{ // The format of macAddr has been converted to host order by C++ return String.format("%1$02x:%2$02x:%3$02x:%4$02x:%5$02x:%6$02x", (macAddr>>40)&0xff, (macAddr>>32)&0xff, (macAddr>>24)&0xff, (macAddr>>16)&0xff, (macAddr>>8) &0xff, (macAddr)&0xff ); }