public String getLatitude(String hex) {
int latStart=0;
String str09 = hex.substring(latStart,latStart+8);
System.out.println("latitude Hex::"+str09);
Integer outDec = Integer.parseInt(str09, 16);
double d = outDec / new Double(1000);
System.out.println("Hex to float>>>" + d);
double lat = d /`enter code here` 3600;
return String.valueOf(lat);
}
public String getLongitude(String hex) throws Exception {
int lonStart=8;
String str09 = hex.substring(lonStart,lonStart+8);
System.out.println("Longitude Hex::"+str09);
Integer outDec = Integer.parseInt(str09,16);
double d = outDec / new Double(1000);
System.out.println("Hex to float>>>" + asHex);
double lon = d / 3600;
return String.valueOf(lon);
}
hexstring - > 090bc3b3ed1b479c
我正在尝试将其转换为纬度和经度。实际输出是 纬度Hex :: 090bc3b3 纬度::: 42.157205277777784
经度Hex :: ed1b479c 经度::: - 88.04980555555555
当我尝试转换为Interger值时,我收到NumberFormatException。这是我正在处理的代码:
我正在异常我将经度十六进制字符串转换为Interger。
public String getLongitude(String hex) throws Exception {
int lonStart=8;
String str09 = hex.substring(lonStart,lonStart+8);
System.out.println("latitude Hex::"+str09);
System.out.println("Longitude Hex::"+str09);
Long outDec = Long.parseLong(str09, 16);
double d = outDec / new Double(1000);
System.out.println("Hex to float>>>" + d);
double lon = d / 3600;
return String.valueOf(lon);
}
我也试过Long,但这就是我得到的:经度::: 1104.9966655555554
答案 0 :(得分:0)
问题在于解析负值。使用090bc3b3ed1b479c
字符串作为输入,您可能得到NumberFormatException: For input string: "ed1b479c"
,因为该数字太大而无法存储在int
中。
Java 8添加了Integer.parseUnsignedInt()
,它将解决您的问题,在更改为无符号转换后输出:
latitude Hex::090bc3b3
Hex to float>>>151765.939
Lat: 42.157205277777784
Longitude Hex::ed1b479c
Hex to float>>>-316979.3
Lon: -88.04980555555555
请注意,Integer.parseUnsignedInt()
仍会返回签名的 int
,但会将输入字符串视为无符号。
修改强>
对于Java 8之前的版本,来自this问题:
int value = (int) Long.parseLong(yourSringHere, 16);