What is the method in the API for database conversion?

What is the method you can use to convert databases in Java? This is something like IntegerToBase (int, base), but I can’t remember.

+5
source share
3 answers

Literally, integer values ​​are not converted from one base to another. Thanks to von Neumann, one of the pioneers of computing who thought that the idea of ​​using base 10 arithmetic on a binary scheme does not make sense, integers are stored in binary format, and we are not trying to change that :-) What are we talking about converting them to strings representing them in some base (another base 10, which is the default) and converting strings to integers in bases other than the base (default) 10. The necessary tools are the static methods of the Integer class.

  • Java provides Integer.toString(int i, int radix)a conversion function that takes an integer and a base (base) and returns that whole string representation in that base.

    String hexRepr = Integer.toString(255, 16)  // returns "FF"
    
  • To go the other way around, that is, from a string representing a number in another database, there is Integer.parseInt(String s, int radix)

    int myNum = Integer.parseInt("FF", 16)  // returns 255
    
  • , radix1, radix2, , :

    int radix1 = 16;  // The input will be parsed assuming base 16 representation
    int radix2 = 4;   // The result will be output using a base 4 representation
    String input = "FF"; // which in base 16 represents the number 255 
    String converted = Integer.toString(Integer.parseInt(radix1Representation, radix1),radix2);  /// returns "3333"  which in base 4 is the number 255
    

API. , , , .


public static String toString(int i, int radix)

radix, .

, Character.MIN_RADIX , Character.MAX_RADIX, 10.

, ASCII '-' ('\u002D'). , .

. , ​​ '0' ('\u0030'); . ASCII:

0123456789abcdefghijklmnopqrstuvwxyz 

'\u0030' '\u0039' '\u0061' '\u007A'. radix N, N radix-N . , ( 16) 0123456789abcdef. , String.toUpperCase():

Integer.toString(n, 16).toUpperCase() 

: http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toString%28int,%20int%29


public static int parseInt(String s, int radix) throws NumberFormatException

, . ( , Character.digit(char, int) ), , ASCII '-' ('\u002D'), ASCII plus '+' ('\u002B'), . .

NumberFormatException , :

  • null .

  • Character.MIN_RADIX Character.MAX_RADIX.

  • , , '-' ('\u002D') '+' ('\u002B'), 1.

  • , , int.

:

parseInt("0", 10)           // returns 0
parseInt("473", 10)         // returns 473
parseInt("+42", 10)         // returns 42
parseInt("-0", 10)          // returns 0
parseInt("-FF", 16)         // returns -255
parseInt("1100110", 2)      // returns 102
parseInt("2147483647", 10)  // returns 2147483647
parseInt("-2147483648", 10) // returns -2147483648
parseInt("2147483648", 10)  // throws NumberFormatException
parseInt("99", 8)           // throws NumberFormatException
parseInt("Kona", 10)        // throws NumberFormatException
parseInt("Kona", 27)        // returns 411787

: http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt%28java.lang.String,%20int%29

+14

Integer.parseInt("101", 2), , 5 ( ).

, , radix, . , , , parseInt (java.lang.String, int) . Integer, , . , Integer : (source)

new Integer(Integer.parseInt(s, radix))

:

public static String toString(int i,int radix)

radix, .

, :

Integer.parseInt(binary_number_in_string, 2);

, :

System.out.println(Integer.toString(2,2));

, radix.


, class Integer , :

public static String toBinaryString(int i) 

string 2 ().

public static String toHexString(int i) 

string 16 (source).

public static String toOctalString(int i) 

string unsigned 8 ().

, , . String integer pass .

+3

The class Integeroffers two utility methods that offer this function, although strings are a transformation environment:

Note that Integerinternally stores values ​​in decimal form (base 10).

For example, to convert 01000 (octal) to 512 (decimal) and vice versa, you can do the following:

String octal = "01000";
int i = Integer.parseInt(octal, 8);
String decimal = Integer.toString(i, 10);
System.out.println(decimal); // prints 512
+3
source

All Articles