我有这个例子:
0 10000101 00111100000000000000000
并希望将其转换为十进制数.
到目前为止,我已经有了获得指数部分的代码:
String[]hex={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
String[]binary={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
String userInput="429E0000";
String result="";
for(int i=0;i<userInput.length();i++)
{
char temp=userInput.charAt(i);
String temp2=""+temp+"";
for(int j=0;j<hex.length;j++)
{
if(temp2.equalsIgnoreCase(hex[j]))
{
result=result+binary[j];
}
}
}
System.out.println(result);
int exponent = Integer.parseInt(result.substring(1,9),2)-127;
System.out.println(exponent);
Run Code Online (Sandbox Code Playgroud)
Java中是否有任何内置命令?
是的,有一个内置命令,intBitsToFloat将32位转换int为a float.您只需将输入解析为int,更简单的方法 - 如果您的输入是十六进制格式 - 将直接使用16进制Integer.parseInt(),然后intBitsToFloat将该位模式转换为a float.
| 归档时间: |
|
| 查看次数: |
3723 次 |
| 最近记录: |