Android 收集的常用的一些函数

来源:互联网 发布:淘宝信用卡分期买手机 编辑:程序博客网 时间:2024/05/23 12:02
class myTools {    /**     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)     */    public static int dip2px(Context context, float dpValue) {        final float scale = context.getResources().getDisplayMetrics().density;        return (int) (dpValue * scale + 0.5f);    }    /**     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp     */    public static int px2dip(Context context, float pxValue) {        final float scale = context.getResources().getDisplayMetrics().density;        return (int) (pxValue / scale + 0.5f);    }    public static byte[] float2byte(float f) {        // 把float转换为byte[]        int fbit = Float.floatToIntBits(f);        byte[] b = new byte[4];        for (int i = 0; i < 4; i++) {            b[i] = (byte) (fbit >> (24 - i * 8));        }        // 翻转数组        int len = b.length;        // 建立一个与源数组元素类型相同的数组        byte[] dest = new byte[len];        // 为了防止修改源数组,将源数组拷贝一份副本        System.arraycopy(b, 0, dest, 0, len);        byte temp;        // 将顺位第i个与倒数第i个交换        for (int i = 0; i < len / 2; ++i) {            temp = dest[i];            dest[i] = dest[len - i - 1];            dest[len - i - 1] = temp;        }        return dest;    }    public static float getFloat(byte[] bytes)    {        return Float.intBitsToFloat(getInt(bytes));    }    public static int getInt(byte[] bytes)    {        return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24));    }    public static short getShort(byte[] b, int index) {        return (short) (((b[index + 1] << 8) | b[index + 0] & 0xff));    }    final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();    public static String bytesToHex(byte[] bytes) {        char[] hexChars = new char[bytes.length * 2];        for ( int j = 0; j < bytes.length; j++ ) {            int v = bytes[j] & 0xFF;            hexChars[j * 2] = hexArray[v >>> 4];            hexChars[j * 2 + 1] = hexArray[v & 0x0F];        }        return new String(hexChars);    }}


工作中常用的函数 记录一下 网上收集的些函数