Java type conversion instance

Source: Internet
Author: User

Java type conversion instance

For example, convert the string content to a floating point number and then restore the original string content.
Import java. io. unsupportedEncodingException; import java. nio. byteBuffer; import java. nio. doubleBuffer; import java. nio. floatBuffer; public class test3 {public static void main (String [] args) {new test3 ();}/*** first Method * // *** 1, integer to byte array conversion ** @ param number * @ return */public static byte [] intToByte (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 (); // Save the highest bit in the temperature bit temp = temp> 8; // shift to the right eight places} return B;}/*** 2, convert byte arrays to Integers ** @ param B * @ return */public static int byteToInt (byte [] B) {int s = 0; for (int I = 0; I <3; I ++) {if (B [I]> = 0) s = s + B [I]; elses = s + 256 + B [I]; s = s * 256;} if (B [3]> = 0) // The last one is not multiplied because s = s + B [3] may overflow. elses = s + 256 + B [3]; return s ;}/ *** 3, character to byte conversion ** @ param ch * @ return */public static byte [] charToByte (char ch) {int temp = (int) ch; byte [] B = new byte [2]; for (int I = B. length-1; I>-1; I --) {B [I] = new Integer (temp & 0xff ). byteValue (); // Save the highest bit in the temperature bit temp = temp> 8; // shift to the right eight places} return B;}/*** 4, byte to character conversion ** @ param B * @ return */public static char byteToChar (byte [] B) {int s = 0; if (B [0]> 0) s + = B [0]; elses + = 256 + B [0]; s * = 256; if (B [1]> 0) s + = B [1]; elses + = 256 + B [1]; char ch = (char) s; return ch;}/*** 5, double-precision floating point to byte conversion ** @ param d * @ return */public static byte [] doubleToByte (double d) {byte [] B = new byte [8]; long l = Double. doubleToLongBits (d); for (int I = 0; I <B. length; I ++) {B [I] = new Long (l ). byteValue (); l = l> 8;} return B;}/*** 6, byte to double Precision Floating Point conversion ** @ param B * @ return */public static double byteToDouble (byte [] B) {long l; l = B [0]; l & = 0xff; l | = (long) B [1] <8); l & = 0 xffff; l | = (long) B [2] <16); l & = 0 xffffff; l | = (long) B [3] <24); l & = 0 xffffffffl; l | = (long) B [4] <32); l & = 0 xffffffffl; l | = (long) B [5] <40 ); l & = 0 xffffffffffl; l | = (long) B [6] <48); l | = (long) B [7] <56 ); return Double. longBitsToDouble (l);}/*** 7, single-precision floating point conversion to byte ** @ param f * @ return */public static byte [] float2byte (float f) {// convert float to byte [] int fbit = Float. floatToIntBits (f); byte [] B = new byte [4]; for (int I = 0; I <4; I ++) {B [I] = (byte) (fbit> (24-I * 8);} // flip the array int len = B. length; // create an array byte [] dest = new byte [len] That is the same as the source array element type; // to prevent modification to the source array, copy a copy of the source array System. arraycopy (B, 0, dest, 0, len); byte temp; // switch the order I and the reciprocal I for (int I = 0; I <len/2; ++ I) {temp = dest [I]; dest [I] = dest [len-I-1]; dest [len-I-1] = temp;} return dest;}/***, 8, convert byte to single precision Floating Point ** @ param B * byte (at least 4 bytes) * @ param index * start position * @ return */public static float byte2float (byte [] B, int index) {int l; l = B [index + 0]; l & = 0xff; l | = (long) B [index + 1] <8 ); l & = 0 xffff; l | = (long) B [index + 2] <16); l & = 0 xffffff; l | = (long) B [index + 3] <24); return Float. intBitsToFloat (l );} /*** convert Byte to String or convert String to Byte *** @ author Administrator *** // *** default character set encoding UTF-8 one Chinese Character occupies three bytes */ private static String CHAR_ENCODE = UTF-8; /*** set the global character encoding ** @ param charEncode */public static void configCharEncode (String charEncode) {CHAR_ENCODE = charEncode ;} /*** @ param str * The Source String converted into a byte array * @ return */public static byte [] StringToByte (String str) {return StringToByte (str, CHAR_ENCODE);}/***** @ param srcObj * convert the Source byte array to a String byte array * @ return */public static String ByteToString (byte [] srcObj) {return ByteToString (srcObj, CHAR_ENCODE );} /*** UTF-8 a Chinese character occupies three bytes ** @ param str * Source String converted into a byte array String * @ return */public static byte [] StringToByte (String str, string charEncode) {byte [] destObj = null; try {if (null = str | str. trim (). equals () {destObj = new byte [0]; return destObj;} else {destObj = str. getBytes (charEncode) ;}} catch (UnsupportedEncodingException e) {e. printStackTrace ();} return destObj ;} /*** @ param srcObj * convert the Source byte array to a String byte array * @ return */public static String ByteToString (byte [] srcObj, String charEncode) {String destObj = null; try {destObj = new String (srcObj, charEncode);} catch (UnsupportedEncodingException e) {e. printStackTrace ();} return destObj. replaceAll (,);}/*** Convert byte [] to hex string ** @ param src * @ return */public static String bytesToHexString (byte [] src) {StringBuilder stringBuilder = new StringBuilder (); if (src = null | src. length <= 0) {return null;} for (int I = 0; I <src. length; I ++) {int v = src [I] & 0xFF; String hv = Integer. toHexString (v); if (hv. length () <2) {stringBuilder. append (0);} stringBuilder. append (hv);} return stringBuilder. toString ();}/*** Convert hex string to byte [] ** @ param hexString * @ return */public static byte [] hexStringToBytes (String hexString) {if (hexString = null | hexString. equals () {return null;} hexString = hexString. toUpperCase (); int length = hexString. length ()/2; char [] hexChars = hexString. toCharArray (); byte [] d = new byte [length]; for (int I = 0; I <length; I ++) {int pos = I * 2; d [I] = (byte) (charToBytes (hexChars [pos]) <4 | charToBytes (hexChars [pos + 1]);} return d ;} /*** Convert char to byte ** @ param c * @ return */private static byte charToBytes (char c) {return (byte) 0123456789ABCDEF. indexOf (c);}/*** method 2 * // *** 1, BYTE to INT ** @ param B * @ return */protected int byteArrayToInt (byte [] B) {return (B [0] <24) + (B [1] & 0xFF) <16) + (B [2] & 0xFF) <8) + (B [3] & 0xFF );} /***, 2, BYTE to SHORT ** @ param B * @ return */protected int byteArrayToShort (byte [] B) {return (B [0] <8) + (B [1] & 0xFF);}/***, 3, SHORT to BYTE data ** @ param s * @ return */protected byte [] Using tobytearray (short s) {byte [] Using Buf = new byte [2]; for (int I = 0; I <2; I ++) {int offset = (partial Buf. length-1-I) * 8; your Buf [I] = (byte) (s >>> offset) & 0xff);} return your Buf ;}/***, 4. Convert INT data to BYTE data ** @ param I * @ return */protected byte [] intToByteArray (int I) {byte [] result = new byte [4]; result [0] = (byte) (I> 24) & 0xFF); result [1] = (byte) (I> 16) & 0xFF ); result [2] = (byte) (I> 8) & 0xFF); result [3] = (byte) (I & 0xFF); return result ;} /***, 5, convert the long type to a byte array ** @ param bb * @ param x * @ param index */public byte [] longToByteArray (long x, int index) {byte [] bb = new byte [8]; bb [index + 7] = (byte) (x> 56); bb [index + 6] = (byte) (x> 48); bb [index + 5] = (byte) (x> 40); bb [index + 4] = (byte) (x> 32); bb [index + 3] = (byte) (x> 24); bb [index + 2] = (byte) (x> 16); bb [index + 1] = (byte) (x> 8); bb [index + 0] = (byte) (x> 0); return bb;}/***, 6, get long ** @ param bb * @ param index * @ return */public long byteArrayToLong (byte [] bb, int index) {return (long) bb [index + 7] & 0xff) <56) | (long) bb [index + 6] & 0xff) <48) | (long) bb [index + 5] & 0xff) <40) | (long) bb [index + 4] & 0xff) <32) | (long) bb [index + 3] & 0xff) <24) | (long) bb [index + 2] & 0xff) <16) | (long) bb [index + 1] & 0xff) <8) | (long) bb [index + 0] & 0xff) <0 ));}/***, 7. float conversion byte ** @ param bb * @ param x * @ param index */public static byte [] floatTobyteArray (float v) {ByteBuffer bb = ByteBuffer. allocate (4); byte [] ret = new byte [4]; FloatBuffer fb = bb. asFloatBuffer (); fb. put (v); bb. get (ret); return ret;}/***, 8, float ** @ param bb * @ param index * @ return */public static float byteArrayToFloat (byte [] v) {ByteBuffer bb = ByteBuffer. wrap (v); FloatBuffer fb = bb. asFloatBuffer (); return fb. get ();}/***, 9, double conversion byte ** @ param bb * @ param x * @ param index */public byte [] doubleToByteArray (double x) {ByteBuffer bb = ByteBuffer. allocate (8); byte [] ret = new byte [8]; DoubleBuffer fb = bb. asDoubleBuffer (); fb. put (x); bb. get (ret); return ret;}/***, 10, float ** @ param bb * @ param index * @ return */public double byteArrayToDouble (byte [] B) {ByteBuffer bb = ByteBuffer. wrap (B); DoubleBuffer fb = bb. asDoubleBuffer (); return fb. get ();} public test3 () {// example: Convert the String Content to a floating point number, and then restore the original String Content = Contiune; System. out. println (for example, convert the string content to a floating point number, and then restore the original string content); // 1, convert the string to a floating point // (1) first convert the string to an array System. out. println (1, string to floating point: (1) first convert string to array Content = + StringToByte (Content); // (2) then convert array to floating point System. out. println (1, the string is converted to a floating point: (1) then the array is converted to a floating point Content = + byteToDouble (StringToByte (Content); // 2, the floating point is converted to a string // (1) first, convert the floating point to the byte array System. out. println (2, floating point to string :( 1) convert floating point to byte array Content = + doubleToByte (byteToDouble (StringToByte (Content); // (2) then convert the byte array to the string System. out. println (2, floating point to string :( 2) convert byte array to string Content = + ByteToString (doubleToByte (byteToDouble (StringToByte (Content )))));}}

The output is as follows:

For example, convert the string content to a floating point number and then restore the original string content.
1. Convert string to floating point: (1) convert string to array Content = [B @ 659e0bfd
1. Convert string to floating point: (1) convert array to floating point Content = 3.949643824749462E180
2. Convert floating point to string: (1) first convert floating point to byte array Content = [B @ 2a139a55
2. convert a floating point to a string: (2) convert the byte array to the string Content = Contiune to restore the original string Content.

 

 

 

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.