how to convert OX12 to decimal [duplicate] - java

This question already has answers here:
Convert hex string to int
(9 answers)
Closed 2 years ago.
I need to convert hexadecimal value which is OX12 to decimal in JAVA,
as I knew hexadecimal is based 16 (1..9, a..f), but in this case, I don't know how to convert with that value.
Can someone help me?
Thanks so much

If the number is a String, you can use the parseInt method of the Integer class with first argument "12" (the number in hex) and second argument 16, the radix, or base, of the number
int number = Integer.parseInt("12", 16);
System.out.println(number);
// output is 18
If the number is not a String, you can simply do the following:
int number = 0x12;
System.out.println(number);
// output is 18

Related

How to take 2 digits number in string and convert it to int [duplicate]

This question already has answers here:
Splitting and converting String to int
(5 answers)
Closed 3 years ago.
String code = "U 12 24";
int s = Integer.parseInt(String.valueOf(code.charAt(2)));
System.out.println(s);
that would be print 1,
however, i want to try print 12 or i mean i want take 2 digits number, but i can't do it because the only way i know is just take one digit number.
how if i want take 12 and convert to int
int s = Integer.parseInt(String.valueOf(code.substring(2, 4)));
If you want to get all digits in a given string, you have to tokenize the string by space and parse every chunk into a number.

Java String.format() leading zeros and 1 decimal place [duplicate]

This question already has answers here:
Add leading zeroes to number in Java? [duplicate]
(5 answers)
Closed 4 years ago.
I need a string like 50 to appear as 050.0. I am using String.format, but I can't figure out how to do leading zeros and a single decimal place at the same time. So far, I have tried String.format("%3.2f", number);, but that isn't working as I still get 50.0 rather than 050.0
Use DecimalFormat to control the number of mandatory digits:
DecimalFormat df = new DecimalFormat("#000.0");
System.out.println(df.format(50)); // 050.0
where
Symbol Location Localized? Meaning
0 Number Yes Digit
# Number Yes Digit, zero shows as absent
You can use StringBuilder class to create a string with number 0 and then append it with you number and insert the decimals at the end.
int num = 50; /*Your number*/
StringBuilder s_num = new StringBuilder("0");
s_num.append(num);
s_num.append(".0");
String f_num = s_num.toString();

How to get a floating-point number from its binary representation in Java? [duplicate]

This question already has answers here:
Convert Double to Binary representation?
(7 answers)
Closed 7 years ago.
I want to create a binary represenation of a floating-point number and be able to parse that number back when needed. By "binary representation" I do not mean "0.00101" but something like "101000101", that is to say, a sequesnce of 0's and 1's with no decimal separator. I need a way to both create such representation in String for a double and to parse a double of a String.
Please do not mention the X Y problem because I do definitly need this method (something like "unsigned binary value").
Thank you in advance.
Convert Double to Binary representation? seemed to solve the problem with parsing double to String but I still need help with doing the opposite: from binary to double.
To convert the bits of a double to a String, you can use Double.doubleToLongBits, which creates a long with the same bits as the double, followed by Long.toBinaryString to convert it to a String with the bits as characters.
double test = 0.5;
long doubleBits = Double.doubleToLongBits(test);
String doubleBitsStr = Long.toBinaryString(doubleBits);
System.out.println(doubleBitsStr);
Output: 11111111100000000000000000000000000000000000000000000000000000
To convert back, use Long.parseLong with a radix of 2 and Double.longBitsToDouble.
doubleBits = Long.parseLong(doubleBitsStr, 2);
test = Double.longBitsToDouble(doubleBits);
System.out.println(test);
Output: 0.5
To convert the bits of a float to a String, you can use Float.floatTointBits, which creates an int with the same bits as the float, followed by Integer.toBinaryString to convert it to a String with the bits as characters.
float test2 = 0.5f;
int intBits = Float.floatToIntBits(test2);
String intBitsStr = Integer.toBinaryString(intBits);
System.out.println(intBitsStr);
Output: 111111000000000000000000000000
To convert back, use Integer.parseInt with a radix of 2 and Float.intBitsToFloat.
intBits = Integer.parseInt(intBitsStr, 2);
test2 = Float.intBitsToFloat(intBits);
System.out.println(test2);
Output: 0.5
Would Integer.toBinaryString(Float.floatToIntBits(yourNumber)); not work?

How to add Leading Zero with getMonth in Java (Android) [duplicate]

This question already has answers here:
How can I pad an integer with zeros on the left?
(18 answers)
Adding a leading zero to a large string in Java
(2 answers)
Closed 8 years ago.
I'm using an int variable:
month = dp.getMonth() + 1;
currently getting an output of "2" and when I do the following:
if (month<10){
month = '0'+month;
};
I get: 50.
Your problem is that your '0' char is being coerced to an integer. Since '0' has an ASCII value of 48, you're getting 48 + 2 = 50.
Note that what you're trying to do won't work - you can't add a leading 0 to month, as month is a number. A leading zero only makes sense in a string representation of a number.
As explained in this answer, here's how to produce a zero-padded number:
String.format("%02d", month);

NumberFormatException from hex number [duplicate]

This question already has answers here:
Convert hex string to int
(9 answers)
Closed 8 years ago.
In my code I have
int i = Integer.parseInt("f8004896",16);
when I run the program it throws a NumberFormatException
java.lang.NumberFormatException: For input string: "f8004896"
What am I doing wrong? This seems relatively straightforward but its not working.
long i = Long.parseLong("f8004896", 16);
System.out.println(i);
System.out.println(Integer.MAX_VALUE);
Output:
4160768150
2147483647
Decimal value for f8004896 is 4160768150 and it's more than 2^31-1 (upper limit for int type). So you should use long instead: Long.parseLong()

Categories