byte工具类

因为工作中需要查看byte[]原始编码,eclipse中debug模式下默认查看byte使用10进制显示,上网查找发现配置为16进制(Hex)显示的办法,记录下来备忘 

在debug模式试图下,Variables试图窗内 View Menu->Java Preferences... 

 

在打开的对话框中Primative Display Options中选中Display hexadecimal values即可 

 

最后在debug时查看byte值就会显示对应16进制编码内容 



byte工具代码:

import java.util.Arrays;

/**
 * *****************************************************************************
 * 
 * @Version 1.0
 * @Description <p>
 *              字节工具类,主要提供JAVA类型的与字节类型的转换功能
 *              <p>
 *              eclipse在Debug显示byte[]数组时候,默认每个byte是10进制的
 */
public class ByteUtil {

	/**
	 * 一个byte转成int32类型数值,前三个字节补0
	 * 
	 * @param b
	 *            一个byte
	 * @return int32
	 */
	public static int byte2Int(byte b) {
		return b & 0xFF;
	}

	public static byte[] spliceArray(final byte[]... sources) {
		int len = 0;
		for (final byte[] arr : sources) {
			len += arr.length;
		}
		final byte[] result = new byte[len];
		int lenCount = 0;
		for (final byte[] arr : sources) {
			System.arraycopy(arr, 0, result, lenCount, arr.length);
			lenCount = lenCount + arr.length;
		}
		return result;
	}

	public static byte[] copyOf(final byte[] source) {
		return Arrays.copyOf(source, source.length);
	}

	public static byte[] byteToBytes(final byte number) {
		return new byte[] { number };
	}

	public static byte booleanToByte(final boolean data) {
		if (data) {
			return 0x1;
		} else {
			return 0x0;
		}
	}

	/**
	 * Short To UINT8.
	 * 
	 * @param number
	 *            {@link Short}
	 * @return byte[1].
	 */
	public static byte[] shortToUBytes(final short number) {
		final byte[] bytes = new byte[1];
		bytes[0] = (byte) (0xFF & number);
		return bytes;
	}

	public static byte[] shortToBytes(final short number) {
		final byte[] bytes = new byte[2];
		bytes[0] = (byte) (0xFF & number);
		bytes[1] = (byte) ((0xFF00 & number) >> 8);
		return bytes;
	}

	public static byte[] charToBytes(final char number) {
		final byte[] bytes = new byte[2];
		bytes[0] = (byte) ((0xFF00 & number) >> 8);
		bytes[1] = (byte) (0xFF & number);
		return bytes;
	}

	/**
	 * char转换为1个byte数组.
	 * 
	 * @param number
	 *            {@link Character}.
	 * @return byte[1].
	 */
	public static byte[] charToCharByte(final char number) {
		final byte[] bytes = new byte[1];
		// bytes[0] = (byte) ((0xFF00 & number) >> 8);
		bytes[0] = (byte) (0xFF & number);
		return bytes;
	}

	/**
	 * int型转换为uint16.
	 * 
	 * @param number
	 *            int .
	 * @return byte[2].
	 */
	public static byte[] intToUBytes(final int number) {
		final byte[] bytes = new byte[2];
		bytes[0] = (byte) (0xFF & number);
		bytes[1] = (byte) ((0xFF00 & number) >> 8);
		// bytes[2] = (byte) ((0xFF0000 & number) >> 16);
		// bytes[3] = (byte) ((0xFF000000 & number) >> 24);
		return bytes;
	}

	public static byte[] intToBytes(final int number) {
		final byte[] bytes = new byte[4];
		bytes[0] = (byte) (0xFF & number);
		bytes[1] = (byte) ((0xFF00 & number) >> 8);
		bytes[2] = (byte) ((0xFF0000 & number) >> 16);
		bytes[3] = (byte) ((0xFF000000 & number) >> 24);
		return bytes;
	}

	public static byte[] longToUBytes(final long number) {
		final byte[] bytes = new byte[4];
		bytes[0] = (byte) (number & 0xff);
		bytes[1] = (byte) ((number >> 8) & 0xff);
		bytes[2] = (byte) ((number >> 16) & 0xff);
		bytes[3] = (byte) ((number >> 24) & 0xff);
		// bytes[4] = (byte) ((number >> 32) & 0xff);
		// bytes[5] = (byte) ((number >> 40) & 0xff);
		// bytes[6] = (byte) ((number >> 48) & 0xff);
		// bytes[7] = (byte) ((number >> 56) & 0xff);
		return bytes;
	}

	public static byte[] longToBytes(final long number) {
		final byte[] bytes = new byte[8];
		bytes[0] = (byte) (number & 0xff);
		bytes[1] = (byte) ((number >> 8) & 0xff);
		bytes[2] = (byte) ((number >> 16) & 0xff);
		bytes[3] = (byte) ((number >> 24) & 0xff);
		bytes[4] = (byte) ((number >> 32) & 0xff);
		bytes[5] = (byte) ((number >> 40) & 0xff);
		bytes[6] = (byte) ((number >> 48) & 0xff);
		bytes[7] = (byte) ((number >> 56) & 0xff);
		return bytes;
	}

	public static byte[] floatToBytes(final float number) {
		final int result = Float.floatToIntBits(number);
		return ByteUtil.intToBytes(result);
	}

	public static byte[] doubleToBytes(final double number) {
		final long result = Double.doubleToLongBits(number);
		return ByteUtil.longToBytes(result);
	}

	/***************************************************************************************/
	/**
	 * @param data
	 * @return
	 */
	public static byte[] parseIntToWord(final int data) {
		final byte ret[] = new byte[2];
		ret[0] = (byte) (data & 0xFF);
		ret[1] = (byte) ((0xFF00 & data) >> 8);
		return ret;
	}

	/***************************************************************************************/
	public static byte bytesToByte(final byte[] bytes) {
		final byte ret = bytes[0];
		return ret;
	}

	public static boolean bytesToBoolean(final byte[] bytes) {
		if ((bytes[0] & 0x01) == 0) {
			return false;
		} else {
			return true;
		}
	}

	public static short bytesToUint8(final byte[] bytes) {
		short ret = bytes[0];
		if (bytes[0] < 0) {
			ret += (short) (0x01 << 8);
		}
		return ret;
	}

	public static int bytesToUint16(final byte[] bytes) {
		int ret = ByteUtil.bytesToShort(bytes);
		if (ret < 0) {
			ret += 0x01 << 16;
		}
		return ret;
	}

	public static long bytesToUint32(final byte[] bytes) {
		long ret = ByteUtil.bytesToInt(bytes);
		if (ret < 0) {
			ret += (long) 0x01 << 32;
		}
		return ret;
	}

	public static short bytesToShort(final byte[] bytes) {
		if (bytes.length == 1) {
			return bytesToUint8(bytes);
		}
		short ret = (short) (bytes[0] & 0xFF);
		ret |= (bytes[1] << 8) & 0xFF00;
		return ret;
	}

	public static char byteToChar(final byte[] bytes) {
		final char ret = (char) (((bytes[0] & 0xFF) << 8));
		return ret;
	}

	public static char bytesToChar(final byte[] bytes) {
		if (bytes.length == 1) {
			return byteToChar(bytes);
		}
		final char ret = (char) (((bytes[0] & 0xFF) << 8) | (bytes[1] & 0xFF));
		return ret;
	}

	public static int bytesToInt(final byte[] bytes) {
		if (bytes.length == 2) {
			return bytesToUint16(bytes);
		}
		int ret = bytes[0] & 0xFF;
		ret |= (bytes[1] << 8) & 0xFF00;
		ret |= (bytes[2] << 16) & 0xFF0000;
		ret |= (bytes[3] << 24) & 0xFF000000;
		return ret;
	}

	public static long bytesToLong(final byte[] bytes) {
		if (bytes.length == 4) {
			return ByteUtil.bytesToUint32(bytes);
		}
		final long l = ((long) bytes[0] & 0xff) | (((long) bytes[1] & 0xff) << 8) | (((long) bytes[2] & 0xff) << 16)
				| (((long) bytes[3] & 0xff) << 24) | (((long) bytes[4] & 0xff) << 32) | (((long) bytes[5] & 0xff) << 40)
				| (((long) bytes[6] & 0xff) << 48) | (((long) bytes[7] & 0xff) << 56);
		return l;
	}

	public static float bytesToFloat(final byte[] b) {
		final int l = ByteUtil.bytesToInt(b);
		return Float.intBitsToFloat(l);
	}

	public static double bytesToDouble(final byte[] b) {
		final long l = ByteUtil.bytesToLong(b);
		return Double.longBitsToDouble(l);
	}

	public static String toHexString(final Number number) {
		return String.format("%02X", number);
	}

	public static byte[] numberToBytes(final byte number, final int length) {
		if (length != 1) {
			throw new RuntimeException("length value must is 1");
		}
		return new byte[] { number };
	}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值