Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of Java data conversion?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report