Android常用的工具方法(3)

1.整数转字节数组

/**
     * 整数转字节数组
     * 
     * @param i
     * @return
     */
    public static byte[] intToByte(int i) {
        byte[] bt = new byte[4];
        bt[0] = (byte) (0xff & i);
        bt[1] = (byte) ((0xff00 & i) >> 8);
        bt[2] = (byte) ((0xff0000 & i) >> 16);
        bt[3] = (byte) ((0xff000000 & i) >> 24);
        return bt;
    }

2.字节数组转整数

    /**
     * 字节数组转整数
     * 
     * @param bytes
     * @return
     */
    public static int bytesToInt(byte[] bytes) {
        int num = bytes[0] & 0xFF;
        num |= ((bytes[1] << 8) & 0xFF00);
        num |= ((bytes[2] << 16) & 0xFF0000);
        num |= ((bytes[3] << 24) & 0xFF000000);
        return num;
    }

3.返回汉字个数

/**
     * 返回汉字个数
     * 
     * @param s
     * @return
     * @throws Exception
     */
    public static int getChineseCount(String s) {// 获得汉字的长度
        char c;
        int chineseCount = 0;
        if (!"".equals("")) {// 判断是否为空
            try {
                s = new String(s.getBytes(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } // 进行统一编码
        }
        for (int i = 0; i < s.length(); i++) {// for循环
            c = s.charAt(i); // 获得字符串中的每个字符
            if (isChineseChar(c)) {// 调用方法进行判断是否是汉字
                chineseCount++; // 等同于chineseCount=chineseCount+1
            }
        }
        return chineseCount; // 返回汉字个数
    }

4.验证一个字符串是否能解析成整数

    /**
     * 验证一个字符串是否能解析成整数
     * 
     * @param numberStr
     * @return
     */
    public static boolean canParseInt(String numberStr) {
        try {
            Integer.parseInt(numberStr);
            return true;
        } catch (NumberFormatException e) {
            return false;
        } catch (NullPointerException e) {
            return false;
        }
    }

5.验证一个字符串是否能解析成双精度浮点数

/**
     * 验证一个字符串是否能解析成双精度浮点数
     * 
     * @param numberStr
     * @return
     */
    public static boolean canParseDouble(String numberStr) {
        try {
            Double.parseDouble(numberStr);
            return true;
        } catch (NumberFormatException e) {
            return false;
        } catch (NullPointerException e) {
            return false;
        }
    }

6.验证一个字符串是否能解析成长整型数

/**
     * 验证一个字符串是否能解析成长整型数
     * 
     * @param numberStr
     * @return
     */
    public static boolean canParseLong(String numberStr) {
        try {
            Long.parseLong(numberStr);
            return true;
        } catch (NumberFormatException e) {
            return false;
        } catch (NullPointerException e) {
            return false;
        }
    }

7.验证一个字符串是否能解析成浮点数

/**
     * 验证一个字符串是否能解析成浮点数
     * 
     * @param numberStr
     * @return
     */
    public static boolean canParseFloat(String numberStr) {
        try {
            Float.parseFloat(numberStr);
            return true;
        } catch (NumberFormatException e) {
            return false;
        } catch (NullPointerException e) {
            return false;
        }
    }

猜你喜欢

转载自blog.csdn.net/lamboo_cn/article/details/52239630