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

📄 typeconverte.java

📁 数据刷新程序,用于不同数据库之间德数据传递
💻 JAVA
字号:
package com.main.apps.common;

import java.io.*;

 
public class TypeConverte
{

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

    private TypeConverte()
    {
    }

    /**
     * 由输入流读取整数
     * 
     * @param is
     *            InputStream 输入流
     * @ruturn result int 读出的整数
     */

    public int myReadInt(InputStream is) throws IOException
    {
        int result = 0;
        int tmp = is.read();
        result = (tmp & 0xff) << 24;
        tmp = is.read();
        result += (tmp & 0xff) << 16;
        tmp = is.read();
        result += (tmp & 0xff) << 8;
        tmp = is.read();
        result += (tmp & 0xff);
        return result;
    }

    /**
     * int型转换 四个字节中1,4对换,2,3对换
     * 
     * @param n ,
     *            int 待转换数
     * @return nResult , int 转换后的整数
     */
    public static int int2Int(int n)
    {
        int nResult = ((byte) (n >>> 24) & 0xff)
                | ((byte) (n >>> 16) & 0xff) << 8
                | ((byte) (n >>> 8) & 0xff) << 16 | ((byte) (n) & 0xff) << 24;
        return nResult;
    }

    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[])
    {
        String result = "";
        for (int i = 0; i < b.length; i++)
            result = result + byteToHexString(b[i]);
        return result;
    }

    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 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 byte[] getBytesRingORPic(String ringOrMap)
    {
        int length = ringOrMap.length() / 2;
        byte[] bMsg = new byte[length];
        for (int i = 0; i < length; i++)
        {
            byte x = Byte.parseByte(ringOrMap.substring(2 * i, 2 * i + 1), 16);
            byte y = Byte.parseByte(ringOrMap.substring(2 * i + 1, 2 * i + 2),
                    16);
            int j = x * 16 + y;
            bMsg[i] = (byte) j;
        }
        return bMsg;
    }

}

⌨️ 快捷键说明

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