读书人

UDP初始

发布时间: 2012-12-25 16:18:28 作者: rapoo

UDP初步

?

UDP初步

?

?

package cn.edu.zcl.util.bytearray; import java.io.IOException; /** * 该类实现不同数据类型与byte数组之间的转换 * @author Legend * */public class ByteArrayUtil { /** * 将指定的int数组转换为byte[]数组 * @param i 指定待转换的int类型变量 * @return 返回转换后的byte数组 * @throws IOException */ public static byte[] toByteArrayFromInt(int i) throws IOException { byte[] b= new byte[4]; b[0] = (byte)i; b[1] = (byte)(i>>8); b[2] = (byte)(i>>16); b[3] = (byte)(i>>24); return b ; } /** * 将指定的byte字节数组转换成int类型数据 * @param byteArray 待转换数组 * @return 转换后的 * @throws IOException */ public static int toIntFromByteArray(byte[] byteArray) throws IOException { int result = 0; result = (int)(byteArray[0]|byteArray[1]<< 8 | byteArray[2] << 16 | byteArray[3] << 24); return result; } public static void main(String[] args) throws IOException{ byte[] b =toByteArrayFromInt(22); int i = toIntFromByteArray(b); System.out.println(i); }}

?

?

对于以上代码在文件ByteArrayUtil.java中。

对于其他类型转换,暂时先不在做讲解,读者可以自行百度,如果后有时间,我会将这部分的内容补充完整。

刚开始没有注意,博文写完之后才发现,所有的类中的没有添加套接字关闭的动作,不过源代码已将上次,就不在修改了。

?

?

读书人网 >编程

热点推荐