How to convert hexadecimal string to single precision floating point in Java?
For example, how to implement:
float f = HexStringToFloat("BF800000"); // f should now contain -1.0
I ask this because I have tried:
float f = (float)(-1.0);
String s = String.format("%08x", Float.floatToRawIntBits(f));
f = Float.intBitsToFloat(Integer.valueOf(s,16).intValue());
But I get the following exception:
java.lang.NumberFormatException: For input string: "bf800000"
public class Test {
public static void main (String[] args) {
String myString = "BF800000";
Long i = Long.parseLong(myString, 16);
Float f = Float.intBitsToFloat(i.intValue());
System.out.println(f);
System.out.println(Integer.toHexString(Float.floatToIntBits(f)));
}
}
You need to convert the hex value to an int (left as an exercise) and then use Float.intBitsToFloat(int)
Related
When I run my code, the error I get says "incompatible types: char cannot be converted to a string"
public class Credit {
public static void main(String[] args) {
long numberLong = Comp122.getLong("Number: ");
String number = numberLong + "";
System.out.println(Integer.parseInt(number.charAt(0)) * 2 + Integer.parseInt(number.charAt(1)) * 2);
System.out.println("VISA");
}
}
char is actually int that holds ASCII number of the given character. So to transform e.g. character 5 to int value, you have to find difference between (char)5 and (char)0.
public class Credit {
public static void main(String[] args) {
long numberLong = 123456789L;
String numberStr = String.valueOf(numberLong);
System.out.println(toInt(numberStr, 0) * 2 + toInt(numberStr, 1) * 2);
System.out.println("VISA");
}
private static int toInt(String numberStr, int index) {
return numberStr.charAt(index) - '0';
}
}
To answer your question title:
Character.toString(char)
This converts a char to a String.
To answer what you are trying to do:
Character.digit(char,int)
Converts a character to the value of the digit it represents in the specified base. In your case you are using base 10.
I want to convert this hex string 48985DDFAF27A0 to double. The result should be 49.1903648.
I tried
Double.longBitsToDouble
Parsing the hex string with parseInt(x, 16) and then using intBitsToFloat
Nothing of the above approaches seems to work.
Need suggestions please
You have to parse the string of hexadecimal number to long. After doing that, you convert to double by using Double.longBitsToDouble. Here is an example of your hex number:
public class Converter {
public static void main(String[] args) {
String hex = "48985DDFAF27A0";
long longHex = parseUnsignedHex(hex);
double d = Double.longBitsToDouble(longHex);
System.out.println(d);
}
public static long parseUnsignedHex(String text) {
if (text.length() == 16) {
return (parseUnsignedHex(text.substring(0, 1)) << 60)
| parseUnsignedHex(text.substring(1));
}
return Long.parseLong(text, 16);
}
}
I need to show some times value on a linear chart and I built a snippet code to convert the values from time to float in Java.
My code doesn't works very well because I can't convert from float to time...
This is my result:
From time to float:
7:43 --> 7.7166667
From float to time:
7.7166667 --> 7:60 (this is wrong...I would to see 7:43)
This is my snippet code:
public class Test {
public static void main(String[] args)
{
String time = "7:43";
float timeFloat = Float.parseFloat(time.replace(":","."));
float resultTo100 = convertTo100(timeFloat);
System.out.println(resultTo100);
String resultTo60 = convertTo60(resultTo100);
System.out.println(resultTo60);
}
public static float convertTo100(float input)
{
String input_string = Float.toString(input);
BigDecimal inputBD = new BigDecimal(input_string);
String hhStr = input_string.split("\\.")[0];
BigDecimal output = new BigDecimal(Float.toString(Integer.parseInt(hhStr)));
output = output.add((inputBD.subtract(output).divide(BigDecimal.valueOf(60), 10, BigDecimal.ROUND_HALF_EVEN)).multiply(BigDecimal.valueOf(100)));
return Float.parseFloat(output.toString());
}
public static String convertTo60(float input)
{
String input_string = Float.toString(input);
BigDecimal inputBD = new BigDecimal(input_string);
String hhStr = input_string.split("\\.")[0];
BigDecimal output = new BigDecimal(Float.toString(Integer.parseInt(hhStr)));
output = output.add((inputBD.subtract(output).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_EVEN)).multiply(BigDecimal.valueOf(60)));
return output.toString().replace(".",":");
}
}
What am I doing wrong?
Thanks!
in your convertTo60 switch the order of divide and multiply operations.
I want to convert a string representing the mantissa portion of a IEEE754 double.
Cannot find if there is such a conversion method in Java, in order to avoid manually adding 1 + 1/2 + 1/4 + 1/8 etc.
|0100000011001010000111110000000000000000000000000000000000000000 --> 13374 in IEEE754
|------------1010000111110000000000000000000000000000000000000000 --> mantissa part
| 1.1010000111110000000000000000000000000000000000000000 --> restoring fixed value 1
String s = "1.1010000111110000000000000000000000000000000000000000"
double mant10 = Double.readFromFloatBinary(s); // does such method exists in Java?
Yes, there are ways to read from a binary representation. But you don't have a representation in an IEEE format.
I would ignore the period and read as a BigInteger base2, then create a value to divide by also using BigInteger:
private static double binaryStringToDouble(String s) {
return stringToDouble(s, 2);
}
private static double stringToDouble(String s, int base) {
String withoutPeriod = s.replace(".", "");
double value = new BigInteger(withoutPeriod, base).doubleValue();
String binaryDivisor = "1" + s.split("\\.")[1].replace("1", "0");
double divisor = new BigInteger(binaryDivisor, base).doubleValue();
return value / divisor;
}
#Test
public void test_one_point_5() {
String s = "1.1";
double d = binaryStringToDouble(s);
assertEquals(1.5, d, 0.0001);
}
#Test
public void test_6_8125() {
String s = "110.1101";
double d = binaryStringToDouble(s);
assertEquals(6.8125, d, 0.0001);
}
#Test
public void test_yours() {
String s = "1.1010000111110000000000000000000000000000000000000000";
double d = binaryStringToDouble(s);
assertEquals(1.632568359375, d, 0.000000000000000001);
}
#Test
public void test_yours_no_trailing_zeros() {
String s = "1.101000011111";
double d = binaryStringToDouble(s);
assertEquals(1.632568359375, d, 0.000000000000000001);
}
How to convert hexadecimal string to single precision floating point in Java?
For example, how to implement:
float f = HexStringToFloat("BF800000"); // f should now contain -1.0
I ask this because I have tried:
float f = (float)(-1.0);
String s = String.format("%08x", Float.floatToRawIntBits(f));
f = Float.intBitsToFloat(Integer.valueOf(s,16).intValue());
But I get the following exception:
java.lang.NumberFormatException: For input string: "bf800000"
public class Test {
public static void main (String[] args) {
String myString = "BF800000";
Long i = Long.parseLong(myString, 16);
Float f = Float.intBitsToFloat(i.intValue());
System.out.println(f);
System.out.println(Integer.toHexString(Float.floatToIntBits(f)));
}
}
You need to convert the hex value to an int (left as an exercise) and then use Float.intBitsToFloat(int)