• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

String to number conversion

How do I convert a string to an integer, float, double etc? Is there a simple function like atoi() in C to do this in Java?

1 Solution
 (Sect. 18) How do I convert a String to an int?
   There are several ways. The most straightforward is:

    String mystring = numString.trim();
    int i = Integer.parseInt(myString);
    long l = Long.parseLong(myString)

    String mystring = numString.trim();
    i = Integer.parseInt(myString,myIntRadix);
  Note 1: There is a gotcha with parseInt - it will throw a
  NumberFormatException for String values in the range "80000000" to "ffffffff".
  You might expect it to interpret them as negative, but it does not. The values
  have to be "-80000000" .. "-ffffffff" to be properly recognized as negative
  values. This is true for all radixes. According to Bug Parade bug report
  4068580, the proper way to generate negative-valued hex Strings for eventual
  use by parseInt() is with Integer.toString(i, 16). Once that high "sign bit"
  is on, without the accompanying character, parseInt() says "too big".
  Note 2: There are similar methods for Byte, Short, and Long. Use
  myString.trim() to get rid of unwanted spaces before the conversion. Some of
  the parse methods can cope with spaces, others can't. They were written by two
  different people.

    int i = Integer.valueOf(my_str).intValue();
  also works but involves the creation of an extra object. Note: the pre-FCS JDK
  1.2 documentation at one point said that parseDouble and parseFloat methods
  were to be introduced, but this does not seem to be the case (see bug
  4160672). JDK 1.2.

    float f = Float.valueOf(my_str).floatValue();
    double d = Double.valueOf(my_str).doubleValue();
  (Sect. 18) How do I convert an int to a string?
   Try any of these:

    String s = String.valueOf(i);

    String s = Integer.toString(i);

    String s = Integer.toString(i, radix);

    // briefer but may result in extra object allocation.
    String s = "" + i;
  Note: There are similar classes for Double, Float, Long, etc.
  (Sect. 18) How do I print the hex value of an int?
   You can print the hex equivalent of an int with:

    int i = 0xf1;
    System.out.println("i is hex " + Integer.toHexString(i) );
  OK, how do I read a hex string into an int?

    int i = Integer.valueOf(myHexString, 16).intValue();
  (Sect. 18) How can you send a function pointer as an argument?
   Simple answer: use a "callback". Make the parameter an interface and pass an
  argument instance that implements that interface.

    public interface CallShow { public void Show( ); }
    public class ShowOff implements CallShow {
        public void Show( ) { .... }

    public class ShowOff2 implements CallShow {
        public void Show( ) { .... }

    public class UseShow {
        CallShow savecallthis;

        UseShow( CallShow withthis ) {
            savecallthis = withthis;

        void ReadyToShow( ) { savecallthis.Show( ); }

    // in some other class that uses all this stuff:
    UseShow use_1 = new UseShow( new ShowOff() );
    UseShow use_2 = new UseShow( new Showoff2() );
  and then the ReadyToShow() method on use_1 or use_2 will call the appropriate
  method, as if you had stored a pointer to the method.

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now