您的位置:首页 > 编程语言 > Java开发

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,为什么不用相同的类型呢?
后面的异常就是解释这个的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: