byte[]与基本类型的转换.doc

byte[]与基本类型的转换

2013-08-10 ???? 8?个评论 ?? ?? 作者:在路上001 收藏 ?? 我要投稿 ?在Java的网络编程中传输的经常是byte数组,但我们实际中使用的数据类型可能是任一种数据类型,这就需要在它们之间相互转换,转换的核心在于将其他类型的数据的每一位转换成byte类型的数据。下面给出相关的转换代码 1.short与byte数组的互转 ? [java] /** * 转换short为byte * * @param b * @param s 需要转换的short * @param index */? public static void putShort(byte b[], short s, int index) {? ???? b[index + 1] = (byte) (s 8);? ???? b[index + 0] = (byte) (s 0);? }? ? /** * 通过byte数组取到short * * @param b * @param index? 第几位开始取 * @return */? public static short getShort(byte[] b, int index) {? ????? return (short) (((b[index + 1] 8) | b[index + 0] 0xff));? }? /** * 转换sho

文档评论(0)

1亿VIP精品文档

相关文档