1, int type and string type of mutual conversion
A, int--String
Recommended For:
public static string ValueOf (int I) Returns the string representation of the int parameter.
B, String--int
Recommended For:
public static int parseint (string S) parses the string argument as a signed decimal integer
1 public classIntegerdemo {2 public Static voidmain (string[] Args) {3 //int--String4 intNumber = 100;5 //Mode 1 Create an empty string and then stitch6String S1 = "" +number ;7System.out.println ("s1:" +s1);8 //mode 2 public static string ValueOf (int I) Returns the string representation of the int parameter. 9 //regardless of the type of number, it can be converted to a string type, which is recommendedTenString s2 =string.valueof (number); oneSystem.out.println ("s2:" +s2); a //Mode 3 - //int--Integer--String -Integer i =NewInteger (number); theString s3 =i.tostring (); -System.out.println ("s3:" +s3); - //Mode 4 - //public static String toString (int i) +String S4 =integer.tostring (number); -System.out.println ("s4:" +s4); +System.out.println ("-----------------"); a at //String--int -String s = "100"; - //Mode 1 - //String--Integer--int -Integer II =NewInteger (s); - //public int Intvalue () in intx =Ii.intvalue (); -System.out.println ("x:" +x); to //mode 2 public static int parseint (string S) parses the string argument as a signed decimal integer + inty =Integer.parseint (s); -System.out.println ("y:" +y); the } *}
2. Common basic Conversion
1. public static String tobinarystring (int I)
2. public static String tooctalstring (int I)
3. public static String tohexstring (int I)
A, Decimal to other binary
Recommended with This:
public static String toString (int i,int radix)
By this, We also saw the range of the binary: 2-36
why? 0,... The 9,a...z number adds up to 36.
B, other binary to decimal
public static int parseint (String S,int radix)
Note: characters in a string must be able to form a back-up, such as ("123", 2), because 3 is out of the range of 2 binary
1 public classIntegerdemo {2 public Static voidmain (string[] Args) {3 //decimal to binary, octal, hex4System.out.println (integer.tobinarystring (100));5System.out.println (integer.tooctalstring (100));6System.out.println (integer.tohexstring (100));7System.out.println ("-------------------------");8 9 //decimal-to-other-binary range: 2-36TenSystem.out.println (integer.tostring (100, 10)); oneSystem.out.println (integer.tostring (100, 2)); aSystem.out.println (integer.tostring (100, 8)); -System.out.println (integer.tostring (100, 16)); -System.out.println (integer.tostring (100, 5)); theSystem.out.println (integer.tostring (100, 7)); -System.out.println (integer.tostring (100, 7)); -System.out.println (integer.tostring (100, 70)); -System.out.println (integer.tostring (100, 1)); +System.out.println (integer.tostring (100, 17)); -System.out.println (integer.tostring (100, 32)); +System.out.println (integer.tostring (100, 37)); aSystem.out.println (integer.tostring (100, 36)); atSystem.out.println ("-------------------------"); - - //other binary to decimal -System.out.println (integer.parseint ("100", 10)); -System.out.println (integer.parseint ("100", 2)); -System.out.println (integer.parseint ("100", 8)); inSystem.out.println (integer.parseint ("100", 16)); -System.out.println (integer.parseint ("100", 23)); to //NumberFormatException because 3 exceeds the range of 2 binary + //System.out.println (integer.parseint ("123", 2)); - } the}
Java 13-4 conversion between integer and string, int, binary conversion