- package com.test;
- import java.nio.ByteBuffer;
- public class ByteUtil {
- /**
- * @param args
- */
- public static void main(String[] args) {
- test2();
- }
- public static void test2()
- {
- short s = -20;
- byte[] b = new byte[2];
- putReverseBytesShort(b, s, 0);
- ByteBuffer buf = ByteBuffer.allocate(2);
- buf.put(b);
- buf.flip();
- System.out.println(getReverseBytesShort(b, 0));
- System.out.println(Short.reverseBytes(buf.getShort()));
- System.out.println("***************************");
- int i = -40;
- b = new byte[4];
- putReverseBytesInt(b, i, 0);
- buf = ByteBuffer.allocate(4);
- buf.put(b);
- buf.flip();
- System.out.println(getReverseBytesInt(b, 0));
- System.out.println(Integer.reverseBytes(buf.getInt()));
- System.out.println("***************************");
- long l = -50;
- b = new byte[8];
- putReverseBytesLong(b, l, 0);
- buf = ByteBuffer.allocate(8);
- buf.put(b);
- buf.flip();
- System.out.println(getReverseBytesLong(b, 0));
- System.out.println(Long.reverseBytes(buf.getLong()));
- System.out.println("***************************");
- }
- public static void test1()
- {
- short s = -20;
- byte[] b = new byte[2];
- putShort(b, s, 0);
- ByteBuffer buf = ByteBuffer.allocate(2);
- buf.put(b);
- buf.flip();
- System.out.println(getShort(b, 0));
- System.out.println(buf.getShort());
- System.out.println("***************************");
- int i = -40;
- b = new byte[4];
- putInt(b, i, 0);
- buf = ByteBuffer.allocate(4);
- buf.put(b);
- buf.flip();
- System.out.println(getInt(b, 0));
- System.out.println(buf.getInt());
- System.out.println("***************************");
- long l = -50;
- b = new byte[8];
- putLong(b, l, 0);
- buf = ByteBuffer.allocate(8);
- buf.put(b);
- buf.flip();
- System.out.println(getLong(b, 0));
- System.out.println(buf.getLong());
- System.out.println("***************************");
- }
- public static void putShort(byte b[], short s, int index) {
- b[index] = (byte) (s >> 8);
- b[index + 1] = (byte) (s >> 0);
- }
- public static void putReverseBytesShort(byte b[], short s, int index) {
- b[index] = (byte) (s >> 0);
- b[index + 1] = (byte) (s >> 8);
- }
- public static short getShort(byte[] b, int index) {
- return (short) (((b[index] << 8) | b[index + 1] & 0xff));
- }
- public static short getReverseBytesShort(byte[] b, int index) {
- return (short) (((b[index+1] << 8) | b[index] & 0xff));
- }
- // ///
- public static void putInt(byte[] bb, int x, int index) {
- bb[index + 0] = (byte) (x >> 24);
- bb[index + 1] = (byte) (x >> 16);
- bb[index + 2] = (byte) (x >> 8);
- bb[index + 3] = (byte) (x >> 0);
- }
- public static void putReverseBytesInt(byte[] bb, int x, int index) {
- bb[index + 3] = (byte) (x >> 24);
- bb[index + 2] = (byte) (x >> 16);
- bb[index + 1] = (byte) (x >> 8);
- bb[index + 0] = (byte) (x >> 0);
- }
- public static int getInt(byte[] bb, int index) {
- return (int) ((((bb[index + 0] & 0xff) << 24)
- | ((bb[index + 1] & 0xff) << 16)
- | ((bb[index + 2] & 0xff) << 8) | ((bb[index + 3] & 0xff) << 0)));
- }
- public static int getReverseBytesInt(byte[] bb, int index) {
- return (int) ((((bb[index + 3] & 0xff) << 24)
- | ((bb[index + 2] & 0xff) << 16)
- | ((bb[index + 1] & 0xff) << 8) | ((bb[index + 0] & 0xff) << 0)));
- }
- // /
- public static void putLong(byte[] bb, long x, int index) {
- bb[index + 0] = (byte) (x >> 56);
- bb[index + 1] = (byte) (x >> 48);
- bb[index + 2] = (byte) (x >> 40);
- bb[index + 3] = (byte) (x >> 32);
- bb[index + 4] = (byte) (x >> 24);
- bb[index + 5] = (byte) (x >> 16);
- bb[index + 6] = (byte) (x >> 8);
- bb[index + 7] = (byte) (x >> 0);
- }
- public static void putReverseBytesLong(byte[] bb, long x, int index) {
- bb[index + 7] = (byte) (x >> 56);
- bb[index + 6] = (byte) (x >> 48);
- bb[index + 5] = (byte) (x >> 40);
- bb[index + 4] = (byte) (x >> 32);
- bb[index + 3] = (byte) (x >> 24);
- bb[index + 2] = (byte) (x >> 16);
- bb[index + 1] = (byte) (x >> 8);
- bb[index + 0] = (byte) (x >> 0);
- }
- public static long getLong(byte[] bb, int index) {
- return ((((long) bb[index + 0] & 0xff) << 56)
- | (((long) bb[index + 1] & 0xff) << 48)
- | (((long) bb[index + 2] & 0xff) << 40)
- | (((long) bb[index + 3] & 0xff) << 32)
- | (((long) bb[index + 4] & 0xff) << 24)
- | (((long) bb[index + 5] & 0xff) << 16)
- | (((long) bb[index + 6] & 0xff) << 8) | (((long) bb[index + 7] & 0xff) << 0));
- }
- public static long getReverseBytesLong(byte[] bb, int index) {
- return ((((long) bb[index + 7] & 0xff) << 56)
- | (((long) bb[index + 6] & 0xff) << 48)
- | (((long) bb[index + 5] & 0xff) << 40)
- | (((long) bb[index + 4] & 0xff) << 32)
- | (((long) bb[index + 3] & 0xff) << 24)
- | (((long) bb[index + 2] & 0xff) << 16)
- | (((long) bb[index + 1] & 0xff) << 8) | (((long) bb[index + 0] & 0xff) << 0));
- }
- }
short,int,long与byte数组之间的转换
最新推荐文章于 2023-07-28 14:50:38 发布