java 如何将十六进制字符串转换为 float 符点型?相互转换
2017-12-06 17:35
531 查看
java 如何将十六进制字符串转换为 float 符点型?
先上代码:
package com.weixiao.network;
/**
* java 如何将十六进制字符串转换为 float 符点型?相互转换
* Hex2Float
* @author 微wx笑
* @date 2017年12月6日下午5:22:10
*/
public class Hex2Float {
public static void main(String[] args) {
String hexString = "46105cec";
Long l = Hex2Float.parseLong(hexString, 16);
Float f = Float.intBitsToFloat(l.intValue());
System.out.println(hexString);
System.out.println(l);
System.out.println(f);
System.out.println(Integer.toHexString(Float.floatToIntBits(f)));
Integer i = Integer.parseInt(hexString, 16);
f = Float.intBitsToFloat(i.intValue());
System.out.println("");
System.out.println(i);
System.out.println(f);
System.out.println(Integer.toHexString(Float.floatToIntBits(f)));
hexString = "-c6105cec";
l = Hex2Float.parseLong(hexString, 16);
f = Float.intBitsToFloat(l.intValue());
System.out.println("");
System.out.println(hexString);
System.out.println(l);
System.out.println(f);
System.out.println(Integer.toHexString(Float.floatToIntBits(f))); // 使用 Long 会输出:ffffffffc6105cec
i = Integer.parseInt(hexString, 16); // 使用 Integer 会抛异常: java.lang.NumberFormatException: For input string: "c6105cec"
f = Float.intBitsToFloat(i.intValue());
System.out.println("");
System.out.println(i);
System.out.println(f);
System.out.println(Integer.toHexString(Float.floatToIntBits(f)));
}
/**
* 代码来自:java.lang.Long
* 因为要跟踪看变量的值,所以要copy出来,或者是去附加源码,否则 eclipse 调试时查看变量的值会提示 xxx cannot be resolved to a variable
* @author 微wx笑
* @date 2017年12月6日下午5:19:40
* @param s
* @param radix
* @return
* @throws NumberFormatException
*/
public static long parseLong(String s, int radix) throws NumberFormatException {
if (s == null) {
throw new NumberFormatException("null");
}
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX");
}
if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX");
}
long result = 0;
boolean negative = false;
int i = 0, len = s.length();
long limit = -Long.MAX_VALUE;
long multmin;
int digit;
if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Long.MIN_VALUE;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s);
if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
multmin = limit / radix;
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++), radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
}
}
/**
* 代码来自:java.lang.NumberFormatException
* NumberFormatException
* @author 微wx笑
* @date 2017年12月6日下午5:20:36
*/
class NumberFormatException extends IllegalArgumentException {
/**
*
*/
private static final long serialVersionUID = 1L;
public NumberFormatException(String s) {
super(s);
}
static NumberFormatException forInputString(String s) {
return new NumberFormatException("For input string: \"" + s + "\"");
}
}对应的输出如下:46105cec
1175477484
9239.23
46105cec
1175477484
9239.23
46105cec
-c6105cec
-3322961132
4.5707135E-4
39efa314
Exception in thread "main" java.lang.NumberFormatException: For input string: "-c6105cec"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:583)
at com.weixiao.network.Hex2Float.main(Hex2Float.java:36)对,你没看错,上面的代码有会抛出异常的!Why?
你注意到将十六进制字符串转换为 float,和将 float 转换为十六进制字符串 的区别了吗?
前面是用 Long,后面是用 Integer,为什么不用相同的类型呢?
后面的异常就是解释这个的。
先上代码:
package com.weixiao.network;
/**
* java 如何将十六进制字符串转换为 float 符点型?相互转换
* Hex2Float
* @author 微wx笑
* @date 2017年12月6日下午5:22:10
*/
public class Hex2Float {
public static void main(String[] args) {
String hexString = "46105cec";
Long l = Hex2Float.parseLong(hexString, 16);
Float f = Float.intBitsToFloat(l.intValue());
System.out.println(hexString);
System.out.println(l);
System.out.println(f);
System.out.println(Integer.toHexString(Float.floatToIntBits(f)));
Integer i = Integer.parseInt(hexString, 16);
f = Float.intBitsToFloat(i.intValue());
System.out.println("");
System.out.println(i);
System.out.println(f);
System.out.println(Integer.toHexString(Float.floatToIntBits(f)));
hexString = "-c6105cec";
l = Hex2Float.parseLong(hexString, 16);
f = Float.intBitsToFloat(l.intValue());
System.out.println("");
System.out.println(hexString);
System.out.println(l);
System.out.println(f);
System.out.println(Integer.toHexString(Float.floatToIntBits(f))); // 使用 Long 会输出:ffffffffc6105cec
i = Integer.parseInt(hexString, 16); // 使用 Integer 会抛异常: java.lang.NumberFormatException: For input string: "c6105cec"
f = Float.intBitsToFloat(i.intValue());
System.out.println("");
System.out.println(i);
System.out.println(f);
System.out.println(Integer.toHexString(Float.floatToIntBits(f)));
}
/**
* 代码来自:java.lang.Long
* 因为要跟踪看变量的值,所以要copy出来,或者是去附加源码,否则 eclipse 调试时查看变量的值会提示 xxx cannot be resolved to a variable
* @author 微wx笑
* @date 2017年12月6日下午5:19:40
* @param s
* @param radix
* @return
* @throws NumberFormatException
*/
public static long parseLong(String s, int radix) throws NumberFormatException {
if (s == null) {
throw new NumberFormatException("null");
}
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX");
}
if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX");
}
long result = 0;
boolean negative = false;
int i = 0, len = s.length();
long limit = -Long.MAX_VALUE;
long multmin;
int digit;
if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Long.MIN_VALUE;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s);
if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
multmin = limit / radix;
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++), radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
}
}
/**
* 代码来自:java.lang.NumberFormatException
* NumberFormatException
* @author 微wx笑
* @date 2017年12月6日下午5:20:36
*/
class NumberFormatException extends IllegalArgumentException {
/**
*
*/
private static final long serialVersionUID = 1L;
public NumberFormatException(String s) {
super(s);
}
static NumberFormatException forInputString(String s) {
return new NumberFormatException("For input string: \"" + s + "\"");
}
}对应的输出如下:46105cec
1175477484
9239.23
46105cec
1175477484
9239.23
46105cec
-c6105cec
-3322961132
4.5707135E-4
39efa314
Exception in thread "main" java.lang.NumberFormatException: For input string: "-c6105cec"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:583)
at com.weixiao.network.Hex2Float.main(Hex2Float.java:36)对,你没看错,上面的代码有会抛出异常的!Why?
你注意到将十六进制字符串转换为 float,和将 float 转换为十六进制字符串 的区别了吗?
前面是用 Long,后面是用 Integer,为什么不用相同的类型呢?
后面的异常就是解释这个的。
相关文章推荐
- Java中如何输出对勾,ASCII编码与字符串相互转换
- 如何相互转换逗号分隔的字符串和List (Java程序员日记 2015/03/06)
- JAVA 字符串与二进制,十六进制 相互转换
- (学习笔记) Java byte[]转二进制字符串,十六进制字符串的相互转换
- java byte[]与十六进制字符串相互转换
- 如何相互转换逗号分隔的字符串和List (Java程序员日记 2015/03/06)
- Java字节、十进制、十六进制、字符串之间的相互转换
- 如何相互转换逗号分隔的字符串和List (Java程序员日记 2015/03/06)
- Java字节、十进制、十六进制、字符串之间的相互转换
- 使用Java完成byte数组与十六进制字符串相互转换
- java中byte数组与十六进制字符串相互转换
- 【转】Java中实现字符串与十六进制编码相互转换
- java如何将一个十六进制字符串转换成ASCII字符串
- 如何相互转换逗号分隔的字符串和List (Java程序员日记 2015/03/06)
- java byte[]与十六进制字符串相互转换
- java中进行二进制,八进制,十六进制,十进制间进行相互转换
- [JAVA]二进制,八进制,十六进制,十进制间进行相互转换
- Java字符串与文件相互转换工具
- java 二进制,八进制,十六进制,十进制间进行相互转换
- [JAVA]二进制,八进制,十六进制,十进制间进行相互转换