Java various types of conversion between __java

Source: Internet
Author: User
Integer to String:
int i = 42;
String str = integer.tostring (i);
Or
String str = "" + I
Double to String:
String str = double.tostring (i);
Long to String:
String str = long.tostring (l);
float to String:
String str = float.tstring (f);
String to Integer:
str = "25";
int i = integer.valueof (str). Intvalue ();
Or
int i = Integer.parseint (str);
String to Double:
Double d = double.valueof (str). Doublevalue ();
String to Long:
Long L = long.valueof (str). Longvalue ();
Or
Long L = Long.parselong (str);
String to float:
Float f = float.valueof (str). Floatvalue ();
Decimal to binary:
int i = 42;
String bin = integer.tobinarystring (i);
Decimal to hexadecimal:
int i = 42;
String hexstr = integer.tostring (i, 16);
Or
String hexstr = integer.tohexstring (i);
or (with leading zeroes and uppercase)
public class Hex {
public static void Main (String args[]) {
int i = 42;
System.out.print
(Integer.tohexstring (0x10000 | i). SUBSTRING (1). toUpperCase ());
}
}
Hexadecimal (String) to Integer:
int i = integer.valueof ("B8da3"). Intvalue ();
Or
ASCII code to String
int i = 64;
String Achar = new Character ((Char) i). ToString ();
Integer to ASCII code (byte)
char c = ' A ';
int i = (int) c; i = = DECIMAL
To extract Ascii codes from a String
String test = "ABCD";
for (int i = 0; i < test.length (); ++i) {
char C = Test.charat (i);
int j = (int) c;
System.out.println (j);
}
Integer to Boolean
b = (i!= 0);
Boolean to Integer
i = (b)? 1:0;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.