The binary in Java

Source: Internet
Author: User

1.Java built-in conversion

integer.tobinarystring (int i);//convert decimal to binary

integer.tooctalstring (int i);//convert decimal into octal

integer.tohexstring (int i);//convert decimal to hexadecimal

Integer.valueof ("2");//convert binary into decimal

Integer.valueof ("376", 8);//convert octal to decimal

Integer.valueof ("FFF");//convert hex to decimal

Or

Integer.parseint ("2");//convert binary into decimal

Integer.parseint ("376", 8);//convert octal to decimal

Integer.parseint ("FFF");//convert hex to decimal

The difference between them two:

A.parseint in the API public static int parseint (string s,int radix) uses the cardinality specified by the second argument to resolve the string argument to a signed integer.

For example: parseint ("0", 10) returns 0 parseint ("473", 10) returns 473 parseint ("0", 10) returns 0 parseint ("-ff", 16) return-255

B.valueof the public static Integer valueOf (String s,int Radix) in the API specifies the cardinality using the second parameter, which acts like parseint. Their only difference is that the return value differs, and valueof returns an Integer object that represents the integer value specified by the string. Instead, parseint returns an integer that is represented by a string parameter of the specified cardinality.

the binary in 2.Java

<1> Convert data type to bytes

Example: 8143 (00000000 00000000 00011111 11001111)

=>byte[]b=[-49,31,0,0]

First (low-end) Byte: 8143>>0*8 & 0xff= (11001111) =207 (or signed-49)

Second (low-end) Byte: 8143>>1*8 & 0xff= (00001111) =31

Third (low-end) Byte: 8143>>2*8 & 0xff= (00000000) =0

Fourth (low-end) Bytes: 8143>>3*8 & 0xff= (00000000) =0

What is called the low end, in fact, is the so-called small and big-endian:

Small-End method: Low-byte emissions in the memory of the address port is the beginning of the value of the address, high-bit bytes emitted in the memory of the higher address.

Big-endian: high-byte emissions at the low address end of memory that is the starting address of the value, low-bit bytes emitted at the high address of the memory

As in the previous example, it's a small-end method.

<2> string conversion to byte array

A. byte array , string

String s;

Byte[] Bs=s.getbytes ();

B. byte array - string

Byte[] Bs=new Byte[int];

String S=new string (BS), or String S=new string (Bs,encode);

Encode refers to the encoding method is Gb2312,utf-8

The binary in Java

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.