Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of Java data conversion?

Shulou Source: shulou.com Published: 2022-06-02 03:46:23 09月26日 Update

Today, I would like to share with you the relevant knowledge of what the method of Java data conversion is, the content is detailed, and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

1 convert string to data

Convert a string to an integer:

String MyNumber = "1234"

Int MyInt = Integer.parseInt (MyNumber)

String can be converted to byte, short, int, float, double, long and other data types.

To refer to the parseXXX of the Byte, Short, Integer, Float, Double, Long classes, respectively

Method.

2 convert data to string

Convert integers to strings:

Int MyInt = 1234

String MyString = "" + MyInt

Other data types can be converted to strings in the same way.

3 conversion from decimal to other decimal

Decimal integers are converted to binary integers, and the result is a string:

Integer.toBinaryString (int I)

Integer and Long provide toBinaryString, toHexString and toOctalString

Method, you can easily convert data into binary, hexadecimal, and octal strings. Function is more

The big enhancement is its toString (int/long I, int radix) method, which can put a decimal

The number is converted to an arbitrary string form.

Data types such as byte, short, float and double. You can use Integer or Long.

Conversion of toBinaryString, toHexString, to OctalString and toString methods based on

Into other binary string forms.

4 other decimal to decimal conversion

The pentadic string 14414 is converted to a decimal integer, and the result is 1234:

System.out.println (Integer.valueOf (14414), 5)

ValueOf (String source, int radix) methods provided by Integer and Long, you can

Converts an arbitrary string into decimal data.

5 conversion from integer to byte array

Public static byte [] toByteArray (int number)

{

Int temp = number

Byte [] b=new byte [4]

For (int I = b.length-1; I >-1; iMel -)

{

B [I] = new Integer (temp & 0xff) .byteValue ()

Temp = temp > > 8

}

Return b

}

Conversion from 6-byte array to integer

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

The difference between short and int is that short is two bytes, while int is four bytes.

Therefore, short integers and bytes can be realized by making minor changes to the sample programs in 5 and 6.

The conversion between arrays.

Convert an 8-byte array to a 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]

Tags: Character string integer data method byte decimal decimal array knowledge article between type binary content form result reference different Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Huawei Shulou Tech Info vpn Apple