SMTH Java FAQ (2) Data Transition (Rookie must see)

zhaozj2021-02-11  186

1 string conversion to data

String to convert into an integer:

String myNumber = "1234";

INT Myint = Integer.Parseint (MyNumber);

The string is converted into byte, short, int, float, double, long and other data types,

Parsexxxx, FLOAT, DOUBLE, LONGEER, FLOAT, DOUBLE, TEGER

method.

2 data conversion into 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 into other credits

The decimal integer is converted into a binary integer, and the return result is a string:

Integer.TobinaryString (INT I);

Integer and Long offer TobinaryString, ToHexString and ToothctalString

Method, it can be convenient to convert data into binary, hexadecimal and octal strings. Function more

Strengthening the big Tostring (int / long i, int RADIX) method can put a ten into

The number is converted into any kind of string form.

Byte, short, float and double data types, you can use Integer or Long

TobinaryString, ToHexString, To OctalString, and Tostring Method Conversion

Beyond the form of other credits.

4 other progress to decimal conversion

The five-part string 14414 converts into a decimal integer, the result is 1234:

System.out.println (Integer.Valueof ("14414", 5);

The valueof (String Source, int RADIX) method provided by Integer and long

Convert arbitrary compact strings into decimal data.

5 integer to byte array conversion

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 Mutual conversion between short integers and byte arrays

The difference between Short and Int is that the short is two bytes, and INT is four bytes.

Therefore, you only need to make a small number of sample programs in 5 and 6, you can implement short integer and bytes.

Mutual conversion between arrays.

8-byte array conversion to double precision floating point

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);

}

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

New Post(0)