Int, Char, double, and byte Conversion

Source: Internet
Author: User

Public class parsetest {</P> <p> // integer to byte array conversion <br/> Public static byte [] inttobyte (INT number) {<br/> int temp = number; <br/> byte [] B = new byte [4]; <br/> for (INT I = B. length-1; I>-1; I --) {<br/> B [I] = new INTEGER (temp & 0xff ). bytevalue (); // Save the highest bit to the lowest Bit <br/> temp = temp> 8; // shift 8 bits to the right <br/>}< br/> return B; <br/>}</P> <p> // converts a byte array to an integer. <br/> Public static int bytetoint (byte [] B) {<br/> int S = 0; <br/> for (INT I = 0; I <3; I ++) {<br/> If (B [I]> = 0) <br/> S = S + B [I]; <br/> else </P> <p> S = S + 256 + B [I]; <br/> S = S * 256; <br/>}< br/> If (B [3]> = 0) // The last one is not multiplied, it may overflow because <br/> S = S + B [3]; <br/> else <br/> S = S + 256 + B [3]; <br/> return s; <br/>}</P> <p> // character to byte conversion <br/> Public static byte [] chartobyte (char ch) {<br/> int temp = (INT) ch; <br/> byte [] B = new byte [2]; <br/> for (INT I = B. length-1; I>-1; I --) {<br/> B [I] = new INTEGER (temp & 0xff ). bytevalue (); // Save the highest bit to the lowest Bit <br/> temp = temp> 8; // shift 8 bits to the right <br/>}< br/> return B; <br/>}</P> <p> // byte to character conversion </P> <p> Public static char bytetochar (byte [] B) {<br/> int S = 0; <br/> If (B [0]> 0) <br/> S + = B [0]; <br/> else <br/> S + = 256 + B [0]; <br/> S * = 256; <br/> If (B [1]> 0) <br/> S + = B [1]; <br/> else <br/> S + = 256 + B [1]; <br/> char CH = (char) s; <br/> return ch; <br/>}</P> <p> // floating point to byte conversion <br/> Public static byte [] doubletobyte (double D) {<br/> byte [] B = new byte [8]; <br/> long l = double. doubletolongbits (d); <br/> for (INT I = 0; I <B. length; I ++) {<br/> B [I] = new long (l ). bytevalue (); <br/> L = L> 8; </P> <p >}< br/> return B; <br/>}</P> <p> // byte to floating point conversion <br/> Public static double bytetodouble (byte [] B) {<br/> long l; </P> <p> L = B [0]; <br/> L & = 0xff; <br/> L | = (long) B [1] <8); <br/> L & = 0 xFFFF; <br/> L | = (long) B [2] <16 ); <br/> L & = 0 xffffff; <br/> L | = (long) B [3] <24); <br/> L & = 0 xffffffffl; <br/> L | = (long) B [4] <32); <br/> L & = 0 xffffffffffl; </P> <p> L | = (long) B [5] <40); <br/> L & = 0 xffffffffffffl; <br/> L | = (long) B [6] <48); </P> <p> L | = (long) B [7] <56); <br/> return double. longbitstodouble (l); <br/>}</P> <p>}

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.