Java和一些windows编程语言如c、c++、delphi所写的网络程序进行通讯时,需要进行高、低字节之间的转换,windows的字节序为低字节开头,linux,unix的字节序为高字节开头;java则无论平台变化,都是高字节开头
/**
* 通信格式转换
*
* Java 和一些windows编程语言如c、c++、delphi所写的网络程序进行通讯时,需要进行相应的转换
* 高、低字节之间的转换
* windows的字节序为低字节开头
* linux,unix的字节序为高字节开头
* java则无论平台变化,都是高字节开头
*/
public class FormatTransfer {
/**
* 将 int转为低字节在前,高字节在后的byte数组
* @param n int
* @return byte[]
*/
public static byte [] toLH( int n) {
byte [] b = new byte [ 4 ];
b[0 ] = ( byte ) (n & 0xff );
b[1 ] = ( byte ) (n >> 8 & 0xff );
b[2 ] = ( byte ) (n >> 16 & 0xff );
b[3 ] = ( byte ) (n >> 24 & 0xff );
return b;
}
}
* 通信格式转换
*
* Java 和一些windows编程语言如c、c++、delphi所写的网络程序进行通讯时,需要进行相应的转换
* 高、低字节之间的转换
* windows的字节序为低字节开头
* linux,unix的字节序为高字节开头
* java则无论平台变化,都是高字节开头
*/
public class FormatTransfer {
/**
* 将 int转为低字节在前,高字节在后的byte数组
* @param n int
* @return byte[]
*/
public static byte [] toLH( int n) {
byte [] b = new byte [ 4 ];
b[0 ] = ( byte ) (n & 0xff );
b[1 ] = ( byte ) (n >> 8 & 0xff );
b[2 ] = ( byte ) (n >> 16 & 0xff );
b[3 ] = ( byte ) (n >> 24 & 0xff );
return b;
}
}