java各种有符号无符号进制转换

来源:互联网 发布:阿莫达非尼 淘宝 编辑:程序博客网 时间:2024/06/14 11:04
 public static Timestamp calendarToTimestamp(Calendar calendar) {        return new Timestamp(calendar.getTimeInMillis());    }    public static Calendar StringToCalendar(String value) {        if (value == null || value.equals("")) {            return null;        }        Calendar dayc1 = new GregorianCalendar();        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        Date daystart = null;        try {            daystart = df.parse(value);        } catch (ParseException e) {            // TODO Auto-generated catch block            e.printStackTrace();            return null;        } // start_date是类似"2013-02-02"的字符串        dayc1.setTime(daystart); // 得到的dayc1就是你需要的calendar了        return dayc1;    }    /**     * @param calendar     * @param format   转换格式 yyyy-MM-dd HH:mm:ss     * @return     */    public static String CalendarToString(Calendar calendar, String format) {        SimpleDateFormat sdf = new SimpleDateFormat(format);        String dateStr = sdf.format(calendar.getTime());        return dateStr;    }    /**     * @return String  返回字符串格式的时间     * @Title: getTime     * @Description: TODO(获取系统时间)     */    public final static String getTime() {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        String strTime = sdf.format(new Date());        return strTime;    }    /**     * @方法说明:字符转为byte     * @创建日期:2013-2-4下午2:22:44     * @参数chr:要转换的字符     * @return说明:返回转换后的byte     */    public final static byte castCharToHexByte(char chr) {        byte chrRet = (byte) -1;        if ((chr >= '0') && (chr <= '9')) {            chrRet = (byte) (chr);        } else if ((chr >= 'A') && (chr <= 'F')) {            chrRet = (byte) (chr - 'A' + 10);        } else if ((chr >= 'a') && (chr <= 'f')) {            chrRet = (byte) (chr - 'a' + 10);        }        return chrRet;    }    /**     * @方法说明:十六进制字符串String转十进制字符串String     * @创建日期:2013-1-31下午1:21:48     * @参数strHexData:传入十六进制字符串     * @return说明:返回十进制字符串     */    public final static String castHexStringToDcmString(String strHexData) {        String strDcmData = null;        Long lngNum = Long.parseLong(strHexData, 16);        strDcmData = String.valueOf(lngNum);        return strDcmData;    }    /**     * @方法说明:十六进制字符串转中文字符串     * @创建日期:2013-1-31下午1:23:21     * @参数strHexData:传入要转成中文的十六进制的字符串     * @return说明:返回中文字符串     */    public final static String castHexStringToHanziString(String strHexData) {        String strRet = null;        byte[] bye = castHexStringToByte(strHexData);        try {            strRet = new String(bye, "gbk"); // 自己调整编码试试看,如UTF-16LE什么的?            // System.out.println(strHexData);        } catch (Exception e) {        }        return strRet;
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre"></span>}</span><pre name="code" class="java"><span style="white-space:pre"></span><pre name="code" class="java"><span style="white-space:pre"></span>/*有符号16进制字符串转换为10进制数字<span style="white-space:pre"></span>*/<span style="white-space:pre"></span>public static short parseHex4(String num) {        if (num.length() != 4) {            throw new NumberFormatException("Wrong length: " + num.length() + ", must be 4.");        }        int ret = Integer.parseInt(num, 16);        ret = ((ret & 0x8000) > 0) ? (ret - 0x10000) : (ret);        return (short) ret;<span style="white-space:pre"></span>}
<span style="white-space:pre"></span>//或者<span style="white-space:pre"></span><pre name="code" class="java"><span style="white-space:pre"></span>String strHexAlt = 8045;//16进制的字符串
int intDcmAlt = Integer.parseInt(strHexAlt, 16);//无符号16进制转换为10进制数字 short shortAlt = (short)(Integer.valueOf(strHexAlt, 16) & 0xffff);//有符号数16进制转换为10进制数字

    /**     * @param strHanzi 传进中文字符串     * @return 返回十六进制的字符串     * @创建时间:2013-2-1下午9:07:02     * @功能说明:中文字符串转为十六进制字符串     */    public final static String castHanziStringToHexString(String strHanzi) {        String strRet = "";        try {            byte[] bye = strHanzi.getBytes("GBK");            strRet = castBytesToHexString(bye);        } catch (Exception ex) {        }        return strRet;    }    /**     * @方法说明:十六进制字符串 保存到byte[]     * @创建日期:2013-1-31下午1:24:22     * @参数strHexData:传入要转成byte[]的十六进制的字符串     * @return说明:返回byte数组     */    public final static byte[] castHexStringToByte(String strHexData) {        if (strHexData != null) {            byte[] bye = new byte[strHexData.length() / 2];            int intLen = strHexData.length();            for (int i = 0, j = 0; i < intLen; i = i + 2, j++) {                byte tmpByte1 = (byte) strHexData.charAt(i);                byte bye1 = (byte) (castCharToHexByte((char) tmpByte1) << 4);// 左移4位                byte tmpByte2 = (byte) strHexData.charAt(i + 1);                byte bye2 = (byte) (castCharToHexByte((char) tmpByte2) & 0xF);                bye[j] = (byte) (bye1 + bye2);// 取低4位然后相加。            }            return bye;        } else {            return null;        }    }    public final static Integer find00FromHexString(String strHexData, int startIndex) {        if (strHexData != null) {            int intLen = strHexData.length();            for (int i = startIndex; i < intLen; i = i + 2) {                String hexdata = strHexData.substring(i, i + 2);                if (hexdata.equals("00")) {                    return i;                }            }            return null;        } else {            return null;        }    }    /**     * @方法说明:byte数组转换为十六进制字符串     * @创建日期:2013-2-4下午2:24:40     * @参数byeData:要转换的byte数组     * @return说明:返回转换后的字符串     */    public final static String castBytesToHexString(byte[] byeData) {        String strRet = null;        int intLen = byeData.length;        for (int i = 0; i < intLen; i++) {            byte byeTemp = byeData[i];            String strHexTemp = Integer.toHexString(byeTemp);            if (strHexTemp.length() > 2) {                strHexTemp = strHexTemp.substring(strHexTemp.length() - 2);            } else if (strHexTemp.length() < 2) {                strHexTemp = "0" + strHexTemp;            }            if (i == 0) {                strRet = strHexTemp;            } else {                strRet = strRet + strHexTemp;            }        }        strRet = strRet.toUpperCase();        return strRet;    }    /**     * @方法说明:十六进制字符串的经度、纬度转换为浮点数字符串的经度、纬度,一般由广嘉GPS协议使用     * @创建日期:2013-2-1下午12:13:49     * @参数strHexTude:要转换的十六进制经度或者纬度数据     * @参数intEffect:保留的小数点位数     * @return说明:返回十进制的小数点字符串经度或者纬度     */    public final static String castHexStringToDcmStringGalaxyTude(            String strHexTude, int intEffect) {        String strFloatTude = null;        Long lngNum = Long.parseLong(strHexTude, 16);        if (lngNum == 0) {            return "0";        }        String strDcmTude = String.valueOf(lngNum);        int intLen = strDcmTude.length();        String strReal = strDcmTude.substring(intLen - intEffect);        String strDecimals = strDcmTude.substring(0, intLen - intEffect);        strFloatTude = strDecimals + "." + strReal;        return strFloatTude;    }    /**     * @方法说明:十六进制字符串的经度、纬度转换为浮点数字符串的经度、纬度,一般由标准协议使用     * @创建日期:2013-2-4下午3:01:04     * @参数strHexTude:要转换的十六进制经度或者纬度数据     * @return说明:返回十进制的小数点字符串经度或者纬度     */    public final static String castHexStringToDcmStringNormTude(            String strHexTude) {        String strFloatTude = null;        strHexTude.replace(" ", "");        String strHexD = strHexTude.substring(0, 2);        String strHexF = strHexTude.substring(2, 4);        String strHexM = strHexTude.substring(4, 6);        String strHexMM = strHexTude.substring(6, 8);        String strDcmD = castHexStringToDcmString(strHexD);// 度        String strDcmF = castHexStringToDcmString(strHexF);// 分        String strDcmM = castHexStringToDcmString(strHexM);// 秒        String strDcmMM = castHexStringToDcmString(strHexMM);// 毫秒        double dblDcmD = Double.parseDouble(strDcmD);        double dblDcmF = Double.parseDouble(strDcmF);        double dblDcmM = Double.parseDouble(strDcmM);        double dblDcmMM = Double.parseDouble(strDcmMM);        double dblTude = dblDcmD + dblDcmF / 60 + (dblDcmM + (dblDcmMM * 0.1))                / (60 * 60);        strFloatTude = String.format("%.7f", dblTude);        return strFloatTude;    }    /**     * @方法说明:私有方法,一般在十进制数字转换十六进制数字的时候使用     * @创建日期:2013-2-1下午4:54:35     * @参数intData:传入16进制中,10--15数字     * @return说明:返回ABCDEF     */    private final static String shuZhiToZhiMu(int intData) {        String strRet = "";        switch (intData) {            case 10:                strRet = "A";                break;            case 11:                strRet = "B";                break;            case 12:                strRet = "C";                break;            case 13:                strRet = "D";                break;            case 14:                strRet = "E";                break;            case 15:                strRet = "F";                break;            default:                strRet = "" + intData;                break;        }        return strRet;    }    /**     * @方法说明:十进制整型转十六进制字符串     * @创建日期:2013-2-1下午5:16:05     * @参数intDcmData:传入要转换的十进制整型     * @return说明:返回十六进制字符串     */    public final static String castDcmIntToHexString(long intDcmData) {        String str = "";        // 1:用a去除以16,得到商和余数        long sun = intDcmData / 16;        int yuShu = (int) (intDcmData % 16);        str = "" + shuZhiToZhiMu(yuShu);        while (sun > 0) {            // 2:继续用商去除以16,得到商和余数            yuShu = (int) (sun % 16);            sun = sun / 16;            // 3:如果商为0,那么就终止            // 4:把所有的余数倒序排列            str = shuZhiToZhiMu(yuShu) + str;        }        return str;    }    /**     * @方法说明:十进制字符串转十六进制字符串     * @创建日期:2013-2-1下午5:14:42     * @参数strDcmData:十进制数字字符串     * @参数intBytes:转化后的字节数     * @return说明:返回十六进制数据字符串     */    public final static String castDcmStringToHexString(String strDcmData,                                                        int intBytes) {        String strRet = null;        long intNum = Long.parseLong(strDcmData, 10);        String strHexData = castDcmIntToHexString(intNum);        String strTempRet = String.valueOf(strHexData);        int intLen = strTempRet.length();        int intTempBytes = intBytes * 2 - intLen;        String strTempByte = null;        for (int i = 0; i < intTempBytes; i++) {            if (strTempByte == null) {                strTempByte = "0";            } else {                strTempByte = strTempByte + "0";            }        }        if (strTempByte == null) {            strRet = strTempRet;        } else {            strRet = strTempByte + strTempRet;        }        return strRet;    }    /**     * // 将 "120502192418" // 转换为“2012-05-02 19:23:18”     *     * @param btim     * @return     */    public static String cast6ByteTimToTimeStr(byte[] btim) {        String timString = castBytesToHexString(btim);        String returntimStr = "20" + timString.substring(0, 2) + "-"                + timString.substring(2, 4) + "-" + timString.substring(4, 6)                + " " + timString.substring(6, 8) + ":"                + timString.substring(8, 10) + ":"                + timString.substring(10, 12);        return returntimStr;    }<pre name="code" class="java">/**     * 十六转二进制     *     * @param hex 十六进制字符串     * @return 二进制字符串     */    public static String castHexStringToBinary(String hex) {        hex = hex.toUpperCase();        String result = "";        int max = hex.length();        for (int i = 0; i < max; i++) {            char c = hex.charAt(i);            switch (c) {                case '0':                    result += "0000";                    break;                case '1':                    result += "0001";                    break;                case '2':                    result += "0010";                    break;                case '3':                    result += "0011";                    break;                case '4':                    result += "0100";                    break;                case '5':                    result += "0101";                    break;                case '6':                    result += "0110";                    break;                case '7':                    result += "0111";                    break;                case '8':                    result += "1000";                    break;                case '9':                    result += "1001";                    break;                case 'A':                    result += "1010";                    break;                case 'B':                    result += "1011";                    break;                case 'C':                    result += "1100";                    break;                case 'D':                    result += "1101";                    break;                case 'E':                    result += "1110";                    break;                case 'F':                    result += "1111";                    break;            }        }        return result;    }


0 0
原创粉丝点击