⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 typestools.java

📁 采用JAVA开发
💻 JAVA
字号:
package com.gctech.sms.sp.cms.util;

import java.util.Date;

/**
 * <p>Title: 工具类</p>
 * <p>Description: 工具类</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: GCTech</p>
 *
 * @author 王红宝
 * @version $Id: TypesTools.java,v 1.3 2005/01/06 06:51:09 lijz Exp $
 */

public class TypesTools {

    private static final char[] HEX_CODE = {
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'a', 'b', 'c', 'd', 'e', 'f'};

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

    private TypesTools() {
    }

    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[]) {
        return byteArrayToHexString(b, 0, b.length);
    }

    public static String byteArrayToHexString(byte b[], int offset, int length) {
        StringBuffer result = new StringBuffer(length * 2);
        for (int i = 0; i < length; i++) {
            int n = b[offset + i];
            if (n < 0)
                n = 256 + n;
            result.insert(2 * i, HEX_CODE[(n >> 4)]);
            result.insert(2 * i + 1, HEX_CODE[n & 0x0f]);
        }
        return result.toString();
    }

    public static void main(String[] args) {
        byte[] data = new byte[]{
            0x00, 0x01, (byte) 0xf1, (byte) 0xff, (byte) 0xfe, (byte) 0x91};
        System.out.println(byteArrayToHexString(data));
    }

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

    public static int byte2int(byte b[]) {
        return b[3] & 0xff | (b[2] & 0xff) << 8 | (b[1] & 0xff) << 16 |
                (b[0] & 0xff) << 24;
    }

    public static short byte2short(byte b[]) {
        return (short) (b[1] & 0xff | (b[0] & 0xff) << 8);
    }

    public static int byte2unsignedShort(byte b[], int offset) {
        return b[offset + 1] & 0xff | (b[offset] & 0xff) << 8;
    }

    public static int unsignedByte2int(byte b) {
        return b & 0xff;
    }

    public static long byte2long(byte b[]) {
        return (long) b[7] & (long) 255 | ((long) b[6] & (long) 255) << 8 |
                ((long) b[5] & (long) 255) << 16
                | ((long) b[4] & (long) 255) << 24 | ((long) b[3] & (long) 255) << 32
                | ((long) b[2] & (long) 255) << 40 | ((long) b[1] & (long) 255) << 48 |
                (long) b[0] << 56;
    }

    public static long byte2long(byte b[], int offset) {
        return (long) b[offset + 7] & (long) 255 |
                ((long) b[offset + 6] & (long) 255) << 8
                | ((long) b[offset + 5] & (long) 255) << 16 |
                ((long) b[offset + 4] & (long) 255) << 24
                | ((long) b[offset + 3] & (long) 255) << 32 |
                ((long) b[offset + 2] & (long) 255) << 40
                | ((long) b[offset + 1] & (long) 255) << 48 | (long) b[offset] << 56;
    }

    public static byte[] int2byte(int n) {
        byte b[] = new byte[4];
        b[0] = (byte) (n >> 24);
        b[1] = (byte) (n >> 16);
        b[2] = (byte) (n >> 8);
        b[3] = (byte) n;
        return b;
    }

    /**
     * n 为待转数据,buf[]为转换后的数据,offset为buf[]中转换的起始点
     * 转换后数据从低到高位
     */
    public static void int2byte(int n, byte buf[], int offset) {
        buf[offset] = (byte) (n >> 24);
        buf[offset + 1] = (byte) (n >> 16);
        buf[offset + 2] = (byte) (n >> 8);
        buf[offset + 3] = (byte) n;
    }

    public static byte[] short2byte(int n) {
        byte b[] = new byte[2];
        b[0] = (byte) (n >> 8);
        b[1] = (byte) n;
        return b;
    }

    public static void short2byte(int n, byte buf[], int offset) {
        buf[offset] = (byte) (n >> 8);
        buf[offset + 1] = (byte) n;
    }

    public static byte[] long2byte(long n) {
        byte b[] = new byte[8];
        b[0] = (byte) (int) (n >> 56);
        b[1] = (byte) (int) (n >> 48);
        b[2] = (byte) (int) (n >> 40);
        b[3] = (byte) (int) (n >> 32);
        b[4] = (byte) (int) (n >> 24);
        b[5] = (byte) (int) (n >> 16);
        b[6] = (byte) (int) (n >> 8);
        b[7] = (byte) (int) n;
        return b;
    }

    public static void long2byte(long n, byte buf[], int offset) {
        buf[offset] = (byte) (int) (n >> 56);
        buf[offset + 1] = (byte) (int) (n >> 48);
        buf[offset + 2] = (byte) (int) (n >> 40);
        buf[offset + 3] = (byte) (int) (n >> 32);
        buf[offset + 4] = (byte) (int) (n >> 24);
        buf[offset + 5] = (byte) (int) (n >> 16);
        buf[offset + 6] = (byte) (int) (n >> 8);
        buf[offset + 7] = (byte) (int) n;
    }

    public static boolean checkMobile(String sMobile) {
        String sF6 = "", sB7 = "", sF2 = "";
        if (sMobile == null)
            return false;
        if (sMobile.length() != 11)
            return false;
        sF6 = sMobile.substring(0, 7);
        sF2 = sMobile.substring(0, 2);
        sB7 = sMobile.substring(7);
        try {
            int iT = Integer.valueOf(sF6).intValue();
            iT = Integer.valueOf(sB7).intValue();
            if (sF2.equals("13"))
                return true;
            else
                return false;
        } catch (Exception ex) {
            return false;
        }
    }

    public static int date2int(Date d) {
        StringBuffer sb = new StringBuffer();
        ;
        int m = d.getMonth()+1;

        sb.append(m);
        int dd = d.getDate();
        if (dd < 10) {
            sb.append("0");
        }
        sb.append(dd);
        int hh = d.getHours();
        if (hh < 10) {
            sb.append("0");
        }
        sb.append(hh);
        int mm = d.getMinutes();
        if (mm < 10) {
            sb.append("0");
        }
        sb.append(mm);

        int ss = d.getSeconds();
        if (ss < 10) {
            sb.append("0");
        }
        sb.append(ss);
        return Integer.parseInt(sb.toString());


    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -