类型转换工具

 

public class BitUtil
{
    private static String HexCode[] = {"0", "1", "2", "3", "4", "5", "6", "7",
            "8", "9", "a", "b", "c", "d", "e", "f"};

    public static void intToBytes(int i, byte[] buf, int offset)
    {
        buf[offset + 3] = (byte) (0xff & i);
        buf[offset + 2] = (byte) ((0xff00 & i) >> 8);
        buf[offset + 1] = (byte) ((0xff0000 & i) >> 16);
        buf[offset + 0] = (byte) ((0xff000000 & i) >> 24);
    }

    public static int bytesToInt(byte abyte0[], int offset)
    {
        return (0xff & abyte0[offset + 0]) << 24
                | (0xff & abyte0[offset + 1]) << 16
                | (0xff & abyte0[offset + 2]) << 8 | 0xff & abyte0[offset + 3];
    }

    public static void longToByte(long i, byte[] b, int offset)
    {
        b[offset + 7] = (byte) (i & 0x00000000000000ff);
        b[offset + 6] = (byte) ((i >> 8) & 0x00000000000000ff);
        b[offset + 5] = (byte) ((i >> 16) & 0x00000000000000ff);
        b[offset + 4] = (byte) ((i >> 24) & 0x00000000000000ff);
        b[offset + 3] = (byte) ((i >> 32) & 0x00000000000000ff);
        b[offset + 2] = (byte) ((i >> 40) & 0x00000000000000ff);
        b[offset + 1] = (byte) ((i >> 48) & 0x00000000000000ff);
        b[offset + 0] = (byte) ((i >> 56) & 0x00000000000000ff);
    }

    public static long byteToLong(byte[] bs, int offset)
    {
        long i1 = (long) (bs[offset + 7] & 0x00ff);
        long i2 = (long) (bs[offset + 6] & 0x00ff) << 8;
        long i3 = (long) (bs[offset + 5] & 0x00ff) << 16;
        long i4 = (long) (bs[offset + 4] & 0x00ff) << 24;
        long i5 = (long) (bs[offset + 3] & 0x00ff) << 32;
        long i6 = (long) (bs[offset + 2] & 0x00ff) << 40;
        long i7 = (long) (bs[offset + 1] & 0x00ff) << 48;
        long i8 = (long) (bs[offset + 0] & 0x00ff) << 56;

        return i1 | i2 | i3 | i4 | i5 | i6 | i7 | i8;
    }

    public static void bytesCopy(byte abyte0[], byte abyte1[], int i, int j,
            int k)
    {
        int i1 = 0;
        for (int l = i; l < abyte0.length && l <= j; l++)
        {
            abyte1[k + i1] = abyte0[l];
            i1++;
        }
    }

    public static String byte2String(byte buf[], int offset, int len)
    {
        byte[] b = new byte[len];
        System.arraycopy(buf, offset, b, 0, len);
        return new String(b).trim();
    }

    public static void stringToByte(String str, byte buf[], int offset, int len)
    {
        byte[] b = str.getBytes();
        System.arraycopy(b, 0, buf, offset, b.length);
    }

    public static int ocsToByte(String str, byte buf[], int offset)
    {
        if (str == null || str.length() == 0)
        {
            buf[offset] = 0;
            return offset + 1;
        }
        else
        {
            byte[] b = str.getBytes();
            System.arraycopy(b, 0, buf, offset, b.length);
            buf[offset + b.length] = 0;
            return offset + b.length + 1;
        }
    }

    public static String byteToOcs(byte[] buf, int offset)
    {
        int i;
        for (i = offset; i < buf.length; i++)
        {
            if (buf[i] == 0)
            {
                break;
            }
        }
        if (i == offset)
        {
            return null;
        }
        else
        {
            byte[] b = new byte[i - offset];
            System.arraycopy(buf, offset, b, 0, b.length);
            return new String(b);
        }
    }

    public static int ocsLenth(String str)
    {
        if (str == null)
        {
            return 1;
        }
        else
        {
            return str.getBytes().length + 1;
        }
    }

    public static String byteToHexString(byte b)
    {
        int n = b;
        if (n < 0)
        {
            n = 256 + n;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return HexCode[d1] + HexCode[d2];
    }

    public static String byteArrayToHexString(byte b[])
    {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < b.length; i++)
        {
            buf.append(byteToHexString(b[i]));
        }
        String result = buf.toString();
        return result;
    }

    public static byte[] hexString2byte(String hexString)
    {
        if (hexString == null || hexString.length() % 2 != 0)
        {
            return null;
        }

        byte[] result = new byte[hexString.length() / 2];

        for (int i = 0; i < hexString.length(); i += 2)
        {
            result[i / 2] = (byte) (Integer.parseInt(
                    hexString.substring(i, i + 2), 16) & 0xff);
        }

        return result;
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值