Convert hexadecimal string to double in Java - java

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);
}
}

Related

How do I convert character to a string

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.

How to use decimal format to hide trailing zero only in integer numbers [duplicate]

This question already has an answer here:
Print float with two decimals unless number is a mathematical integer
(1 answer)
Closed 2 years ago.
I need format numbers like this:
23.0 -> 23
23.20 -> 23.20
23.00 -> 23
23.11 -> 23.11
23.2 -> 23.20
23.999 -> 24
23.001 -> 23
1345.999 -> 1346 // Edited the question to add this from the OP's comment
Here is my code:
java:
public static String toPrice(double number) {
DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols();
formatSymbols.setGroupingSeparator(' ');
DecimalFormat format = new DecimalFormat("#,###,###.##", formatSymbols);
return format.format(number);
}
kotlin:
fun Double.toPrice(): String = DecimalFormat("#,###,###.##", DecimalFormatSymbols().apply {
groupingSeparator = ' '
}).format(this)
But for input 23.20 or 23.2 I get the result 23.2. This is wrong for me. I need 23.20. Which string pattern should I use to achieve this result? Please, help me.
public static String toPrice(double number) {
if (number == (int) number) {
return Integer.toString((int) number);
} else {
return String.format("%.2f", number);
}
}
EDITTo include thousand separator
public static String toPrice(double number) {
if (number == (int) number) {
return String.format("%,d",(int)number);
} else {
return String.format("%,.2f", number);
}
}
Update
In case you need to format 1345.999 as 1346 (as mentioned in your comment), you can do so by adding a couple of extra steps in the original answer:
Format the number with the pattern, #,###,###.00 and then remove all commas so that it can be parsed back to double.
Parse the converted string into double and follow the rest of the solution as mentioned in the original answer.
Demo:
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String[] args) {
System.out.println(getFromattedNumber(1234567.0));
System.out.println(getFromattedNumber(1234567.20));
System.out.println(getFromattedNumber(1234567.01));
System.out.println(getFromattedNumber(1234567.00));
System.out.println(getFromattedNumber(1345.999));
}
static String getFromattedNumber(double number) {
NumberFormat format = new DecimalFormat("#,###,###.00");
double num = Double.parseDouble(format.format(number).replace(",", ""));
// Format for integer decimal numbers
NumberFormat formatInt = new DecimalFormat("#,###,###");
if ((int) num == num) {
return formatInt.format(number);
} else {
return format.format(number);
}
}
}
Output:
1,234,567
1,234,567.20
1,234,567.01
1,234,567
1,346
Original answer
You will need to use two formats as shown below:
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String[] args) {
System.out.println(getFromattedNumber(1234567.0));
System.out.println(getFromattedNumber(1234567.20));
System.out.println(getFromattedNumber(1234567.01));
System.out.println(getFromattedNumber(1234567.00));
}
static String getFromattedNumber(double number) {
// Format for integer decimal numbers
NumberFormat format1 = new DecimalFormat("#,###,###");
// Format for non-integer decimal numbers
NumberFormat format2 = new DecimalFormat("#,###,###.00");
if ((int) number == number) {
return format1.format(number);
} else {
return format2.format(number);
}
}
}
Output:
1,234,567
1,234,567.20
1,234,567.01
1,234,567

Conversion of decimal into binary using recursion

I want to convert the decimal number into a binary number using recursion in java. I tried a lot but unable to do it. Here is my code:
public class DecimalToBinary {
public static void main(String[] args) {
System.out.println(conversion(2));
}
public static int conversion(int n) {
return reconversion(n);
}
public static int reconversion(int n) {
if(n <= 0)
return 0;
else {
return (int) (n/2 + conversion(n/2));
}
}
}
Integer values are already in binary. The fact that they appear as digits 0 thru 9 when you print them is because they are converted to a string of decimal digits. So you need to return a String of binary digits like so.
public static String conversion(int n) {
String b = "";
if (n > 1) {
// continue shifting until n == 1
b = conversion(n >> 1);
}
// now concatenate the return values based on the logical AND
b += (n & 1);
return b;
}

Hexadecimal Calculations for Calculator [duplicate]

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)

How to convert hex string to float in Java?

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)

Categories