Example: Converting the contents of a string to a floating-point number and then restoring the contents of the original string
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 = num Ber;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 at the lowest bit temp = temp >> 8; Move right 8 bits}return b;} /** * 2, byte array to integer conversion * * @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 + n + b[i];s = s * 256;} if (B[3] >= 0)//The last one is not multiplied because it may overflow s = s + b[3];elses = s + n + 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 by te[2];for (int i = b.length-1; i >-1; i--) {B[i] = new Integer (temp & 0xff). Bytevalue ();//Save the highest bit at the lowest bit temp = temp >> 8; Move right 8 bits}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];els Es + + + b[0];s *= 256;if (b[1] > 0) s + = b[1];elses + + + 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 &= 0xffff;l |= ((long) b[2] <<), L &= 0xffffff;l |= ((long) b[3] << 24 L &= 0xffffffffl;l |= ((long) b[4] <<), L &= 0xffffffffffl;l |= ((long) b[5] << +); l &= 0xfff Fffffffffl;l |= ((long) b[6] <<), L |= ((long) b[7] <<); 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.fl Oattointbits (f); byte[] B = new Byte[4];for (int i = 0; i < 4; i++) {B[i] = (byte) (Fbit >> (24-i * 8));} Flip array int len = b.length;//Create an array of the same type as the source array element byte[] Dest = new byte[len];//in order to prevent the source array from being modified, copy a copy of the source array system.arraycopy (b, 0, DE St, 0, Len); byte temp;//will be the first and the penultimate I exchange for (int i = 0; i < LEN/2; ++i) {temp = Dest[i];d est[i] = dest[len-i-1];d E St[len-i-1] = temp;} return dest;} /** *, 8, Bytes converted to single-precision floating-point * * @param b * bytes (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 &= 0xFF Ff;l |= (Long) B[index + 2] << l &= 0xffffff;l |= ((long) B[index + 3] <<); return float.intbitstof Loat (l);} /** * Converts a byte to a string or converts a string to byte * * @author Administrator * *//** * default Character Set encoding UTF-8 a kanji account of three bytes */privAte static String Char_encode = "UTF-8";/** * Set Global character encoding * * @param charencode */public static void Configcharencode (Strin G charencode) {char_encode = Charencode;} /** * @param str * string converted to byte array * @return */public static byte[] Stringtobyte (String str) {return Stringtoby Te (str, char_encode);} /** * * @param srcobj * Source byte array converted to String byte array * @return */public static string bytetostring (byte[] srcobj) {Retu RN bytetostring (Srcobj, Char_encode);} /** * UTF-8 A kanji account of three bytes * * @param str * string converted into a byte array * @return */public static byte[] Stringtobyte (string s TR, 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 * Source byte array converted to a byte array of String * @return */public static string bytetostring (byte[] srcobj, string c Harencode) {String destobj = null;try {destobj = new String (srcobj, Charencode);} catch (Unsupportedencodingexception e) {e.printstacktrace ();} return Destobj.replaceall ("n", "");} /** * Convert byte[] to hex string * * @param src * @return */public static string bytestohexstring (byte[] src) {Stringbu Ilder 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 Hexstrin g) {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) "0123456789ABC DEF ". IndexOf (c);} /** * Second Method *//** * 1,byte to int * * @param b * @return */protected int bytearraytoint (byte[] b) {return (b[0] << 24) + ((b[1] & 0xFF) << + ((b[2] & 0xFF) << 8) + (B[3] & 0xFF);} /** *, 2,byte turn short * * @param b * @return */protected int bytearraytoshort (byte[] b) {return (b[0] << 8) + (B[1] & Amp 0xFF);} /** *, 3,short to byte data * * @param s * @return */protected byte[] Shorttobytearray (short s) {byte[] shortbuf = new byte[2]; for (int i = 0; i < 2; i++) {int offset = (Shortbuf.length-1-i) * 8;shortbuf[i] = (byte) ((S >>> offset) & Amp 0xFF);} return shortbuf;} /** *, 4,int data to byte data * * @param i * @return */protected byte[] Inttobytearray (int i) {byte[] result = new Byte[4];result [0] = (byte) ((I >>) & 0xFF); result[1] = (byte) ((I >>) & 0xFF); result[2] = (byte) ((I >> 8) & 0xFF); result[3] = (byte) (I & 0xFF); return result;} /** *, 5, convert type long to 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 >> n); Bb[index + 6] = (byte) (x >> n); Bb[index + 5] = (byte) (x >> Bb[index + 4] = (byte) (x >> +), Bb[index + 3] = (byte) (x >> +); Bb[index + 2] = (byte) (x > > Bb[index + 1] = (byte) (x >> 8); Bb[index + 0] = (byte) (x >> 0); return BB;} /** *, 6, via byte array take to long * * @param BB * @param index * @return */public long bytearraytolong (byte[] bb, int index) {retur N ((((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 convert 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, obtained by a byte array float * * @param BB * @param index * @return */public static float bytearraytofloat (byte[] v) {BYTEBUF fer bb = bytebuffer.wrap (v); Floatbuffer fb = Bb.asfloatbuffer (); return Fb.get ();} /** *, 9,double convert byte * * @param BB * @param x * @param index */public byte[] Doubletobytearray (double x) {Bytebuffer bb = Bytebuffer.allocate (8); byte[] ret = new Byte[8];D oublebuffer fb = Bb.asdoublebuffer (); fb.put (x); Bb.get (ret); return RET;} /** *, 10, obtained by a byte array float * * @param BB * @param index * @return */public double bytearraytodouble (byte[] b) {Bytebuffer bb = Bytebuffer.wrap (b);D Oublebuffer fb = Bb.asdoublebuffer (); return Fb.get ();} PublicTest3 () {///Example: Converts the contents of a string to a floating-point number, and then restores the original string contents of string content = "Contiune"; System.out.println ("Example: convert the contents of a string to a floating-point number, and then restore the original string content");//1, the string to float//(1) The string into the array System.out.println ("1, the string to floating point: (1) First convert the string to array content= "+ stringtobyte (Content));//(2) then convert the array to floating point System.out.println (" 1, string to floating point: (1) and then convert the array to floating point content= " + bytetodouble (Content));//2, float to string//(1) First float to byte array System.out.println ("2, float to string: (1) First the floating point to byte array content= "+ Doubletobyte (bytetodouble (Content))),//(2) and then the byte array to the string System.out.println (" 2, float to string: (2) then convert byte array to string content= "+ bytetostring (Doubletobyte (bytetodouble (Content))));}}
The printing results are as follows:
Example: Converting the contents of a string to a floating-point number and then restoring the contents of the original string
1, string to floating point: (1) Convert string to array first Content=[[email protected]
1, the string is converted to floating point: (1) and then the array is converted to floating point content=3.949643824749462e180
2, floating point to String: (1) Convert floating point to byte array first content=[[email protected]
2, floating point into a string: (2) The byte array is then converted to string content=contiune, that is, the original string content is restored.
<p style= "margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px; border:0px; Text-indent:0em; background:transparent; " ></p>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Type Conversion instance