java各种有符号⽆符号进制转换
public static Timestamp calendarToTimestamp(Calendar calendar) {
return new TimeInMillis());
}
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.Time());
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 ><span > </span>}</span><pre name="code" class="java"><span white-space:pre"> </span>*/
<span > </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 > </span>}
<span > </span>//或者<span > </span><pre name="code" class="java"><span > </span>St
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 = "";
String strRet = "";
try {
byte[] bye = Bytes("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 = HexString(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 = UpperCase();
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;
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:05java switch case string
* @参数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;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论