Saturday 7 April 2012

Converting Numbers to Strings in Java

Sometimes you need to convert a number to a string because you need to operate on the value in its string form. There are several easy ways to convert a number to a string:

 

Concatenate number with an empty string


For example:
int i=10;

// Concatenate i with an empty string "".
String s1 = "" + i;

Now string s1 contains the value "10".

Use the static valueOf() method in String class


String class provides the following overloaded static valueOf() methods to construct the string representation of the primitive types.


        public static String valueOf(char c)
        public static String valueOf(boolean b)
        public static String valueOf(int i)
        public static String valueOf(long l)
        public static String valueOf(float f)
        public static String valueOf(double d)

For example:
        String bool = String.valueOf(false);
        String ch = String.valueOf('c');
        String i = String.valueOf(10);
        String l = String.valueOf(100L);
        String f = String.valueOf(3.14f);
        String d = String.valueOf(3.14);

Use Wrapper classes Static toString() method


To convert a primitive type to a string you can use the following methods offered by the wrapper classes.

 
Wrapper
Static toString() method
Byte
toString(byte value)
Short
toString(short value)
Integer
toString(int value)
toString(int value, int radix)
Long
toString(long value)
toString(long value, int radix)
Float
toString(float value)
Double
toString(double value)
Character
toString(char c)
Boolean
toString(boolean b)
Void
None

For example:
 
        String b = Byte.toString((byte) 5);

        String s = Short.toString((short) 25);

        String i1 = Integer.toString(100);
        String i2 = Integer.toString(100, 2);

        String l1 = Long.toString(1000);
        String l2 = Long.toString(1000, 8);

        String f = Float.toString(3.14f);

        String d = Double.toString(3.14);

        String bool = Boolean.toString(false);

        String ch = Character.toString('c');


Notice that Integer and Long toString() methods take an additional argument, int radix, which indicates in what base (for example binary, octal, or hexadecimal) the first argument is represented.

For example,

String i1 = Integer.toString(1001,2);

Here converts binary 1001 to the corresponding decimal value 9 and assigns the string form of 9 to variable i1.


toXxxString() method(Binary, Hexadecimal, Octal)


The Integer and Long wrapper classes let you convert numbers in base 10 to other bases. These conversion methods, toXxxString(), take an int or long, and return a String representation of the converted number.The toXxxString() methods offered by the wrapper classes are given below.


Wrapper
Static toXxxString() method
Integer
toHexString(int value)
toOctalString(int value)
toBinaryString(int value)
Long
toHexString(long value)
toOctalString(long value)
toBinaryString(long value)
Float
toHexString(float value)
Double
toHexString(float value)
 
For example:

public class Main {

    public static void main(String args[]) {

        String i_binary = Integer.toBinaryString(15);
        String i_octal = Integer.toOctalString(15);
        String i_hex = Integer.toHexString(15);

        System.out.println("15 in hex=" + i_hex);
        System.out.println("15 in octal=" + i_octal);
        System.out.println("15 in binary=" + i_binary);

        String l_hex = Long.toHexString(100);
        String l_octal = Long.toOctalString(100);
        String l_binary = Long.toBinaryString(100);

        System.out.println("100 in hex=" + l_hex);
        System.out.println("100 in octal=" + l_octal);
        System.out.println("100 in binary=" + l_binary);

        String f_hex = Float.toHexString(3.14f);
        String d_hex = Double.toHexString(3.14);

        System.out.println("3.14f in hex=" + f_hex);
        System.out.println("3.14 in hex=" + d_hex);

    }

}



The output is

15 in hex=f
15 in octal=17
15 in binary=1111
100 in hex=64
100 in octal=144
100 in binary=1100100
3.14f in hex=0x1.91eb86p1
3.14 in hex=0x1.91eb851eb851fp1

No comments:

Post a Comment