Java basic data type conversion

xiaoxiao2021-03-05  26

1 String Convert into data string Convert to integers: string myNumber = "1234"; int myint = integer.parseint (MyNumber); string is converted into Byte, Short, int, float, double, long, etc., can Refer to the Parsexxx method for Byte, Short, Integer, Float, Double, and long classes.

2 Data Conversion into a string integer converted into strings: int myint = 1234; string mystring = "" myint; other data types can be converted into strings using the same method.

3 Decimal to other credited conversion decimal integers into binary integers, return results are a string: integer.tobinaryString (INTEGER and LONG), providing TobinaryString, ToHexString, and ToolString methods, it is convenient to convert data into binary , Hexadecimal and octal strings. The function is more powerful is its Tostring (int / long i, int RADIX) method, which can convert a decimal number to any of the string form. Byte, short, float, and double data types, you can use Integer or long tobinaryString, ToHExString, To Octalstring, and TOHEXSTRING, TO Octalstring, and TOSTRING methods.

4 Others Enter the decimal conversion five-input translation 514441 converted into a decimal integer, the result is 1234: System.out.Println (Integer.Valueof ("14414", 5); Integer and Long provided Valueof (String Source, INT RADIX) method, any of the entered strings can be converted into decimal data.

5 integer to byte arrays Translation public static Byte [] TobyTearray (int Number) {Int Temp = Number; Byte [] b = new byte [4]; for (int i = B.Length - 1; i> -1 ; i -) {b [i] = new integer (Temp & 0xFF) .Bytevalue (); Temp = Temp >> 8;} return b;}

6-byte array to integer conversion public static int tointeger (byte [] b) {INT S = 0; for (int i = 0; i <3; i ) {if (b [i]> 0) s = s B [I]; ELSE S = S 256 B [I]; S = S * 256;} IF (B [3]> 0) S = S B [3]; ELSE S = S 256 B [3]; Return S;

7 The difference between the mutual conversion between the short integer and the byte array SHORT and INT is that the short is two bytes, and INT is four bytes. Therefore, it is only necessary to make a small change in the sample program in 5 and 6, which can achieve mutual conversion between the short integer and the byte array.

8-byte array converted into double precision floating point number public double Todouble (byte [] b) {long L = 0; double d = new double (0.0); l = b [0]; l | = ((long) b [ 1] << 8); L | = ((long) B [2] << 16); L | = ((long) b [3] << 24); L | = ((long) b [4] << 32); L | = ((long) B [5] << 40); L | = ((long) B [6] << 48); l | = (long) B [7] << 56); Return D.longBitStodouble (L); 9 Boolean type conversion into a string first method is:

Boolean Bool = true; string s = new boolean (bool) .tostring (); // Transform Bool to object S.Equals ("true"); / * where the Totring method is a inheritance method. All classes in Java are Object's inheritance. An important way to object is toString, which is used to convert objects to strings. * /

The second method is:

Boolean Bool = True; String S = String.Valueof (bool);

First, from the length of the code, the second method is obviously compacted than the first method; second, the first method introduces a completely unnecessary object during the conversion, so this is relative to the second method Incidentally, the waste of memory space is greatly slowed down. Therefore, it is recommended to use the second method.

10 Transformation between digital types and digital classes Byte B = 169; BYTE BO = New Byte (b); b); b = bo.bytevalue ();

Short T = 169; Short to = New Short (T); t = to.shortvalue ();

INT I = 169; Integer IO = New Integer (i); i = o.intvalue ();

Long L = 169; long lo = new long (l); l = lo.longvalue ();

Float f = 169f; float fo = new float (f); f = fo.floatvalue ();

Double d = 169f; double dobj = new double (d); d = dobj.doublevalue ();

*********************************************************** *********************************************************** *

1 How to convert a string string into an integer int?

A. There are two ways:

1). INT i = integer.parseint ([string]); or

I = integer.parseint ([String], [INT RADIX]);

2). INT i = integer.valueof (my_str) .intValue ();

Note: The string is transformed into Double, Float, and long.

2 How to convert integer int converted into a string string?

A. Have a variety of methods:

String s = String.Valueof (i);

2.) String s = integer.tostring (i); 3.) String s = "" i;

Note: Double, Float, long-converted to string is similar.

Java data type conversion YNNIEBO [Favorit]

Keyword type conversion

Somewhere

This is an example that is said to be conversion of data digital in Java. For everyone to learn

Package cn.com.lwkj.rts.Register;

Import java.sql.date;

Public class typechange {

Public typechange () {

}

// Change the string type to the int TYPE

Public Static int StringToint (String IntStr)

{

Integer integer;

Integer = integer.valueof (INTST);

Return Integer.intValue ();

}

// Change int Type to the String Type

Public Static String INTSTOSTOSTOSTRING (INT Value)

{

Integer Integer = New Integer (Value);

Return integer.toString ();

}

// Change the string type to the float type

Public static float stringtofloat (String floatstr)

{

Float floatee;

FLOATEE = float.valueof (floatstr);

Return floatee.floatvalue ();

}

// Change the float type to the string type

Public Static String floattostring (Float Value)

{

Float floatee = new float (value);

Return floatee.tostring ();

}

// Change the string type to the sqldate type

Public static java.sql.date stringtodate (String DateStr)

{

Return java.sql.date.valueof (dateStr);

}

// change the sqldate type to the string type

Public Static String DateTString (java.sql.date datee)

{

Return Datee.toString ();

}

Public static void main (string [] args)

{

Java.sql.date day;

Day = TypeChange.StringTodate ("2003-11-3");

String strday = typechange.datetostring (day);

System.out.println (strDay);

}

}

Data type conversion function in Java

Although it can be found in the Java API, make a backup.

String-> Byte

Byte Static Byte ParsebyTe (String S)

BYTE-> STRING

Byte Static String Tostring (byte b)

Char-> string

Character Static String to String (Char C)

String-> short

Short Static Short Parseshort (String S) Short-> String

Short Static String Tostring (SHORT S)

String-> Integer

Integer Static Int PaSeint (String S)

Integer-> String

Integer Static String Tostring (INT I)

String-> long

Long Static Long Parselong (String S)

Long-> String

Long Static String Tostring (long i)

String-> float

Float Static Float Parsefloat (String S)

FLOAT-> String

Float Static String Tostring (Float F)

String-> DOUBLE

Double Static Double Parsedouble (String S)

Double-> String

Double Static String Tostring (double

*********************************************************** *********************************************************

String-> Bytebyte Static Byte ParsebyTe (String S)

Byte-> String Byte Static String Tostring (byte B)

Char-> string character static string to string (char c)

String-> Short Short Static Short Parseshort (String S)

Short-> String Short Static String TSTRING (Short S)

String-> Integer Integer Static Int Parseint (String S)

Integer-> String

Integer Static String Tostring (INT i)

String-> long

Long Static Long Parselong (String S)

Long-> String Long Static String Tostring (long i)

String-> float

Float Static Float Parsefloat (String S)

FLOAT-> String

Float Static String Tostring (Float F)

String-> Double Double Static Double Parsedouble (String S)

Double-> String

Double Static String Tostring (Double D)

转载请注明原文地址:https://www.9cbs.com/read-31807.html

New Post(0)