如何从 Java 中的二进制表示中获取浮点数?
How to get a floating-point number from its binary representation in Java?
我想创建浮点数的二进制表示,并能够在需要时解析该数字。 "binary representation" 我不是指“0.00101
”,而是类似于“101000101
”,也就是说,0
和 1
的序列s 没有小数点分隔符。我需要一种方法来在 String
中为 double
创建这样的表示并解析 String
的 double
。
请不要提及 X Y 问题,因为我确实需要这种方法(类似于 "unsigned binary value")。
提前谢谢你。
Convert Double to Binary representation? 似乎解决了将 double
解析为 String
的问题,但我仍然需要帮助来做相反的事情:从二进制到 double
.
Integer.toBinaryString(Float.floatToIntBits(yourNumber));
行不通吗?
要将 double
的位转换为 String
,您可以使用 Double.doubleToLongBits
, which creates a long
with the same bits as the double
, followed by Long.toBinaryString
将其转换为 String
,并将位作为字符。
double test = 0.5;
long doubleBits = Double.doubleToLongBits(test);
String doubleBitsStr = Long.toBinaryString(doubleBits);
System.out.println(doubleBitsStr);
输出:11111111100000000000000000000000000000000000000000000000000000
要转换回来,请使用 Long.parseLong
with a radix of 2
and Double.longBitsToDouble
。
doubleBits = Long.parseLong(doubleBitsStr, 2);
test = Double.longBitsToDouble(doubleBits);
System.out.println(test);
输出:0.5
要将 float
的位转换为 String
,您可以使用 Float.floatTointBits
, which creates an int
with the same bits as the float
, followed by Integer.toBinaryString
将其转换为 String
,并将位作为字符。
float test2 = 0.5f;
int intBits = Float.floatToIntBits(test2);
String intBitsStr = Integer.toBinaryString(intBits);
System.out.println(intBitsStr);
输出:111111000000000000000000000000
要转换回来,请使用 Integer.parseInt
with a radix of 2
and Float.intBitsToFloat
。
intBits = Integer.parseInt(intBitsStr, 2);
test2 = Float.intBitsToFloat(intBits);
System.out.println(test2);
输出:0.5
我想创建浮点数的二进制表示,并能够在需要时解析该数字。 "binary representation" 我不是指“0.00101
”,而是类似于“101000101
”,也就是说,0
和 1
的序列s 没有小数点分隔符。我需要一种方法来在 String
中为 double
创建这样的表示并解析 String
的 double
。
请不要提及 X Y 问题,因为我确实需要这种方法(类似于 "unsigned binary value")。
提前谢谢你。
Convert Double to Binary representation? 似乎解决了将 double
解析为 String
的问题,但我仍然需要帮助来做相反的事情:从二进制到 double
.
Integer.toBinaryString(Float.floatToIntBits(yourNumber));
行不通吗?
要将 double
的位转换为 String
,您可以使用 Double.doubleToLongBits
, which creates a long
with the same bits as the double
, followed by Long.toBinaryString
将其转换为 String
,并将位作为字符。
double test = 0.5;
long doubleBits = Double.doubleToLongBits(test);
String doubleBitsStr = Long.toBinaryString(doubleBits);
System.out.println(doubleBitsStr);
输出:11111111100000000000000000000000000000000000000000000000000000
要转换回来,请使用 Long.parseLong
with a radix of 2
and Double.longBitsToDouble
。
doubleBits = Long.parseLong(doubleBitsStr, 2);
test = Double.longBitsToDouble(doubleBits);
System.out.println(test);
输出:0.5
要将 float
的位转换为 String
,您可以使用 Float.floatTointBits
, which creates an int
with the same bits as the float
, followed by Integer.toBinaryString
将其转换为 String
,并将位作为字符。
float test2 = 0.5f;
int intBits = Float.floatToIntBits(test2);
String intBitsStr = Integer.toBinaryString(intBits);
System.out.println(intBitsStr);
输出:111111000000000000000000000000
要转换回来,请使用 Integer.parseInt
with a radix of 2
and Float.intBitsToFloat
。
intBits = Integer.parseInt(intBitsStr, 2);
test2 = Float.intBitsToFloat(intBits);
System.out.println(test2);
输出:0.5