Converting Numbers to and from Strings

One of the most common programming’ chores is converting the string representation of a number into its internal, binary format. Fortunately, Java provides an easy way’ to accomplish this. The Byte, Short, Integer, and Long classes provide the parse Byte(}, parse Short(), parse Int(), and parse Long( methods, respectively. These methods turn the byte, short, int, or long equivalent of the numeric string with which they are called. (Similar methods also exist for the Float and Double classes.)   following program demonstrates parse Int ). It sums a list of integers entered by the Tiler. It reads the integers using read Line( ) and uses sparseness ) to convert these · strings.into int equivalents.

To convert hole number into a clinical string, use the versions of to string( ) defined in the Byte, Short, Integer, or Long classes. The Integer and Long classes also provide the methods to Binary String ( ), to Hex String( ), and to Octal String( ), which convert a value into a binary, hexadecimal, or octal string; respectively. The following program demonstrates binary, hexadecimal, and ‘octal conversion:

The output-of this program is shown here:

19648 in binary: 100110011000000
19648 in octal: 46300
19648 in hex decim: Rocco

[vfb id=1]

Share This