Home  >  Article  >  Java  >  Conversion between bases in java

Conversion between bases in java

高洛峰
高洛峰Original
2016-11-30 15:01:321097browse

//Convert decimal to other bases

Integer.toHexString(10); //Convert 10 to hexadecimal and return the string type

Integer.toOctalString(10); //Convert 10 to octal and return String type

Integer.toBinaryString(10); //Convert 10 to binary and return string type

//Convert other bases to decimal

//Convert hexadecimal to decimal, for example: 0xFFFF

Integer.valueOf("FFFF",16).toString(); //The valueOf() method returns the Integer type, and calling toString() returns the string

Integer.parseInt("FFFF",16); // Returns the int basic data type

Integer.toString(0xFFFF); //This method can directly pass in the basic data type representing hexadecimal numbers, and the method returns a string

//Convert octal to decimal, for example: 017

Integer.valueOf("17",8).toString(); //The valueOf() method returns the Integer type, and calling toString() returns a string

Integer.parseInt("17",8); //Return int basic data type

Integer.toString(017); //This method can directly pass in the basic data type representing octal numbers, and the method returns a string

//Convert binary to decimal, for example: 0101

Integer. valueOf("0101",2).toString(); //The valueOf() method returns the Integer type, and calling toString() returns a string

Integer.parseInt("0101",2); //Returns the int basic data type

//For conversion between binary, octal and hexadecimal, you can first convert to decimal, and then use the corresponding method of converting from decimal to multi-decimal for conversion

//For example, convert hexadecimal 0xFF For binary

Integer.toBinaryString(Integer.valueOf("FF",16));

//or

Integer.toBinaryString(Integer.parseInt("FF",16));

//For To input a string representing hexadecimal, you need to intercept the numeric substring first, and then use the valueOf() or parseInt() method to convert it to decimal

//For example, enter 0xFF

String s = "0xFF";

Integer.valueOf(s.subString(2,s.length()),16);

//For the valueOf method, it can be used for boxing of basic data types and conversion between polydecimal and decimal.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn