Java:如何将二进制值的字符串转换为浮点数,反之亦然? [英] Java: How to convert a String of Binary values to a Float and vice-versa?

查看:743
本文介绍了Java:如何将二进制值的字符串转换为浮点数,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将12345.12346f的浮点值转换为二进制值的字符串,即0011010101010101,反之亦然?

解决方案

我不确定这是你想要的,但这里有一个解决方案,以浮点的IEEE 754浮点双格式位布局的二进制表示(它基本上是一个float):


  int intBits = Float.floatToIntBits(yourFloat); 
String binary = Integer.toBinaryString(intBits);

反向过程:

  int intBits = Integer.parseInt(myString,2); 
float myFloat = Float.intBitsToFloat(intBits);


How do I convert the float value of 12345.12346f to a String of binary values, i.e. "0011010101010101", and vice-versa?

解决方案

I'm not sure it is what you want, but here's a solution to have the binary representation of the IEEE 754 floating-point "double format" bit layout for a float (it is basically the memory representation of a float) :

int intBits = Float.floatToIntBits(yourFloat); 
String binary = Integer.toBinaryString(intBits);

For the reverse procedure :

int intBits = Integer.parseInt(myString, 2);
float myFloat = Float.intBitsToFloat(intBits);

这篇关于Java:如何将二进制值的字符串转换为浮点数,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆