将一个四个字节float类型转化为内存存储格式步骤为:
31…0
31位 符号位 正数是0 负数是1
30-23位 指数部分:
0-22位 尾数部分:
下面计算指数部分 和尾数部分如何存入
1.先将这个实数的绝对值转化为二进制格式,整数转为二进制,小数转为二进制
例如8.25转为二进制
1000.01
0.4转换为二进制
0.0110011001100110…
2.将这个二进制格式实数的小数点左移或者右移n位,直到小数点移动到第一个有效数字右边
例如8.25转为二进制
1000.01=1.000012^3 小数点左移三位
0.4转换为二进制
0.0110011001100110…=1.1001100110011002^-2 小数点右移两位
3.移位后 从小数点右边第一位开始数数出二十三位数字放入第22到第0位
例如8.25
22位->0位的顺序 00001000000000000000000
例如0.4
22位->0位的顺序 10011001100110011001100 超过二十三位截取掉
4.指数部分+127 转换为二进制 存入30位到23位
例如 8.25 指数部分为 3
3+127=130 转换为二进制 10000010
例如0.4指数部分为-2
-2+127=125 转换为二进制1111101 七位不够八位 高位补0
java 代码实现
package com.gac.okio;
import java.util.HashMap;
import java.util.Stack;
public class Covert {
int negative;
int thirty;
HashMap map = new HashMap<>();
public Covert(){
map.put("0000","0");
map.put("0001","1");
map.put("0010","2");
map.put("0011","3");
map.put("0100","4");
map.put("0101","5");
map.put("0110","6");
map.put("0111","7");
map.put("1000","8");
map.put("1001","9");
map.put("1010","A");
map.put("1011","B");
map.put("1100","C");
map.put("1101","D");
map.put("1110","E");
map.put("1111","F");
}
//对浮点数进行分割
String[] floatToIntAndFloat(String num){
if(num.toCharArray()[0]=='-'){
negative = 1;
num=num.replace("-","");
}else{
negative = 0;
}
return num.split(",");
}
//整数部分 小数部分转换为32位的二进制
String combine(String biLeft,String biRight){
int zhishu = 0;
String rear=""; //0-22位存放的值
String medim="";//23位到29位
System.out.println("left:"+biLeft+" right:"+biRight);
if(biLeft.length() > 1){
//左移 30为存1
thirty=1;
zhishu = biLeft.length()-2;
String temp = biLeft.substring(1,biLeft.length());
//System.out.println(temp);
rear=temp+biRight;
if(rear.length() > 23){
rear = rear.substring(0,23);
}else{
for(int i =rear.length(); i<=22;i++){
rear+="0";
}
}
// System.out.println(rear);
medim = convertBinary(zhishu+"");
// System.out.println(medim);
String tempmedim="";
for(int i = 7; i > medim.length(); i--){
tempmedim+="0";
}
medim=tempmedim+medim;
}else if(biLeft.length() == 1 && biLeft.equals("1")){
//不需要移动位数 30位0 剩下的补1 指数部分 23-29位
//127-指数 23-29位正好七位最大值127
thirty = 0;
String temp = biLeft.substring(1,biLeft.length());
//System.out.println(temp);
rear=temp+biRight;
if(rear.length() > 23){
rear = rear.substring(0,23);
}else{
for(int i =rear.length(); i<=22;i++){
rear+="0";
}
}
// System.out.println(rear);
medim = "1111111";
}else{//0 0 1111101 100 1100 1100 1100 1100 1100
//右移 30位存0
thirty= 0;
int index = biRight.indexOf("1");//找到第一个为1的位置索引 右移动的位数
zhishu = 127-(index+1);
String temp = biRight.substring(index+1,biRight.length());
//System.out.println(temp);
rear=temp;
if(rear.length() > 23){
rear = rear.substring(0,23);
}else{
for(int i =rear.length(); i<=22;i++){
rear+="0";
}
}
// System.out.println(rear);
medim = convertBinary(zhishu+"");
// System.out.println(medim);
String tempmedim="";
for(int i = 7; i > medim.length(); i--){
tempmedim+="0";
}
medim=tempmedim+medim;
}
System.out.println("medim:"+medim.length()+" rear:"+rear.length());
return negative+""+thirty+""+medim+rear;
}
//小数部分转换为二进制
String converPoint(String right){
right="0."+right;
float f = Float.parseFloat(right);
System.out.println(f);
StringBuilder stringBuilder = new StringBuilder();
int i = 0;
while (f*2!=0){
f=f*2;
if(f>= 1){
stringBuilder.append(1);
f=f-(int)f;
}else{
stringBuilder.append(0);
}
i++;
if(i >= 23){
break;
}
}
return stringBuilder.toString();
}
//整数部分转化为二进制字符串
String convertBinary(String left){
int int_left = Integer.parseInt(left);
Stack stack = new Stack<>();
while (int_left/2!=0){
stack.push(int_left%2);
int_left=int_left/2;
}
stack.push(int_left);
StringBuffer sb =new StringBuffer();
while (!stack.isEmpty()){
sb.append(stack.pop());
}
return sb.toString();
}
public void execute(String str_float){
Covert covert = new Covert();
String[] strs = covert.floatToIntAndFloat(str_float);
String res = covert.combine(covert.convertBinary(strs[0]),covert.converPoint(strs[1]));
System.out.println(res);
String[] str8 = new String[8];
StringBuilder sb_res = new StringBuilder();
if(res.length() == 32){
char[] chars = res.toCharArray();
System.out.println(chars.length);
for(int i = 0; i < 8; i++){
str8[i]="";
str8[i]+=chars[4*i]+"";
str8[i]+=chars[4*i+1]+"";
str8[i]+=chars[4*i+2]+"";
str8[i]+=chars[4*i+3]+"";
}
for(int i = 0; i < 8; i++){
System.out.println(str8[i]);
sb_res.append(map.get(str8[i]));
}
}
System.out.println(sb_res.toString());
}
public static void test1(){
String[] strs =new Covert().floatToIntAndFloat("12,0098");
System.out.println(strs.length);
for(int i = 0; i < strs.length; i++){
System.out.println(strs[i]);
}
}
public static void test2(){
System.out.println(new Covert().convertBinary("11"));
System.out.println(new Covert().convertBinary("20"));
System.out.println(new Covert().convertBinary("3"));
}
public static void test3(){
System.out.println(new Covert().converPoint("25"));
System.out.println(new Covert().converPoint("5"));
System.out.println(new Covert().converPoint("4"));
}
public static void test4(){
Covert covert = new Covert();
String[] strs = covert.floatToIntAndFloat("8,25");
String res = covert.combine(covert.convertBinary(strs[0]),covert.converPoint(strs[1]));
System.out.println(res.length());
}
public static void test5(){
Covert covert = new Covert();
covert.execute("8,25");
}
public static void main(String[] args){
test5();
}
}```