Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Java Articles
Page 77 of 450
Java Program to convert hexadecimal number to decimal number
Use the parseInt() method with the second parameter as 16 since it is the radix value. The parseInt() method has the following two forms.static int parseInt(String s) static int parseInt(String s, int radix)To convert hex string to decimal, use the 2nd syntax and add radix as 16, since hexadecimal radix is 16.Integer.parseInt("12", 16)Examplepublic class Demo { public static void main( String args[] ) { // converting to decimal System.out.println(Integer.parseInt("444", 16)); } }Output1092
Read MoreJava Program to convert octal number to decimal number
Use the parseInt() method with the second parameter as 8 since it is the radix value. The parseInt() method has the following two forms.static int parseInt(String s) static int parseInt(String s, int radix)To convert octal to decimal, use the 2nd syntax and add radix as 8, since octal radix is 8.Integer.parseInt("25", 8)The following is an example.Examplepublic class Demo { public static void main( String args[] ) { // converting to decimal System.out.println(Integer.parseInt("25", 8)); } }Output21
Read MoreJava Program to convert decimal integer to hexadecimal number
Use the toHexString() method to convert decimal to hexadecimal. The method returns a string representation of the integer argument as an unsigned integer in base 16. The following characters are used as hexadecimal digits:0123456789abcdef.The following is the syntax.String toHexString(int i)It has only single parameter.i − This is an integer to be converted to a string.Examplepublic class Demo { public static void main( String args[] ) { int dec = 45; System.out.println("Decimal = "+dec); // converting to hex System.out.println(Integer.toHexString(dec)); } }OutputDecimal = 45 2d
Read MoreHow to convert Decimal to Hexadecimal in Java
To convert decimal to hexadecimal, use any of the two methods i.e.Integer.toHexString() − It returns a string representation of the integer argument as an unsigned integer in base 16.Integer.parseInt() − It allows you to set the radix as well, for example, for hexadecimal set it as 16.Let us see an example now to convert decimal to hexadecimal using Integer.toHexString() method.Examplepublic class Demo { public static void main( String args[] ) { int dec = 158; System.out.println(Integer.toHexString(dec)); } }Output9eLet us see an example now to convert decimal to hexadecimal using Integer.parseInt() method.Examplepublic class ...
Read MoreParse and format a number to decimal in Java
To parse and format a number to decimal, try the following code.Examplepublic class Demo { public static void main( String args[] ) { int val = Integer.parseInt("5"); String str = Integer.toString(val); System.out.println(str); } }Output5In the above program, we have used the Integer.parseInt() and Integer.toString() method to convert a number to decimal.int val = Integer.parseInt("5"); String str = Integer.toString(val);The toString() method above represents a value in string and formats.
Read MoreParse and format to hexadecimal in Java
To parse and format to hexadecimal in Java, we have used the BigInteger class. The java.math.BigInteger class provides operations analogues to all of Java's primitive integer operators and for all relevant methods from java.lang.Math.In the below example, we have taken BigInteger and created an object. With that, some values have been set by us in the arguments as the hexadecimal and the radix i.e. 16 for hexadecimal.BigInteger bi = new BigInteger("2ef", 16);Let us see an example.Exampleimport java.math.*; public class Demo { public static void main( String args[] ) { BigInteger bi = new BigInteger("2ef", 16); ...
Read MorePass an integer by reference in Java
To pass an integer by reference in Java, try the following code −Examplepublic class Demo { public static void display(int[] arr) { arr[0] = arr[0] + 50;; } public static void main(String[] args) { int val = 50; int[] myArr = { val }; display(myArr); System.out.println(myArr[0]); } }Output100In the above program, a custom method is created, which pass an int array.int val = 50; int[] myArr = { val }; display(myArr);In the method, we have performed mathematical operation on the value of array.public static void display(int[] arr) { arr[0] = arr[0] + 50;; }The updated value is then displayed in the main.System.out.println(myArr[0]);
Read MoreReverse an Integer in Java
To reverse an integer in Java, try the following code −Exampleimport java.lang.*; public class Demo { public static void main(String[] args) { int i = 239, rev = 0; System.out.println("Original: " + i); while(i != 0) { int digit = i % 10; rev = rev * 10 + digit; i /= 10; } System.out.println("Reversed: " + rev); } }OutputOriginal: 239 Reversed: 932In the above program, we have the following int value, which we will reverse.int i = 239;Now, loop through until the value is 0. Find the remainder and perform the following operations to get the reverse of the given integer 239.while(i != 0) { int digit = i % 10; rev = rev * 10 + digit; i /= 10; }
Read MoreHow to assign int value to char variable in Java
To assign int value to a char variable in Java would consider the ASCII value and display the associated character/ digit.Here, we have a char.char val;Now assign int value to it.val = 77;Now, when you will print the value of “val”, it would display the character associate with the value (ASCII) 77.The following is the complete example.Examplepublic class Demo { public static void main(String []args) { char val; val = 77; System.out.print("Value: "); System.out.println(val); } }OutputValue: M
Read MoreJava Program to validate if a String contains only numbers
To validate if a String has only numbers, you can try the following codes. We have used the matches() method in Java here to check for number in a string.Examplepublic class Demo { public static void main(String []args) { String str = "978"; System.out.println("Checking for string that has only numbers..."); System.out.println("String: "+str); if(str.matches("[0-9]+") && str.length() > 2) System.out.println("String has only numbers!"); else System.out.println("String consist of characters as well!"); } }OutputChecking for string that has only numbers... String: ...
Read More