標籤:java 類型轉換
例:將字串內容轉為浮點數,再還原出原字串內容
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();}/** * 第一種方法 *//** * 1,整數到位元組數組的轉換 * * @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(); // 將最高位儲存在最低位temp = temp >> 8; // 向右移8位}return b;}/** * 2,位元組數組到整數的轉換 * * @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) // 最後一個之所以不乘,是因為可能會溢出s = s + b[3];elses = s + 256 + b[3];return s;}/** * 3,字元到位元組轉換 * * @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(); // 將最高位儲存在最低位temp = temp >> 8; // 向右移8位}return b;}/** * 4,位元組到字元轉換 * * @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,雙精確度浮點到位元組轉換 * * @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,位元組到雙精確度浮點轉換 * * @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] << 16);l &= 0xffffff;l |= ((long) b[3] << 24);l &= 0xffffffffl;l |= ((long) b[4] << 32);l &= 0xffffffffffl;l |= ((long) b[5] << 40);l &= 0xffffffffffffl;l |= ((long) b[6] << 48);l |= ((long) b[7] << 56);return Double.longBitsToDouble(l);}/** * 7,單精確度浮點轉換為位元組 * * @param f * @return */public static byte[] float2byte(float f) {// 把float轉換為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));}// 翻轉數組int len = b.length;// 建立一個與源數組元素類型相同的數組byte[] dest = new byte[len];// 為了防止修改源數組,將源數組拷貝一份副本System.arraycopy(b, 0, dest, 0, len);byte temp;// 將順位第i個與倒數第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,位元組轉換為單精確度浮點 * * @param b * 位元組(至少4個位元組) * @param index * 開始位置 * @return */public static float byte2float(byte[] b, int index) {int l;l = b[index + 0];l &= 0xff;l |= ((long) b[index + 1] << 8);l &= 0xffff;l |= ((long) b[index + 2] << 16);l &= 0xffffff;l |= ((long) b[index + 3] << 24);return Float.intBitsToFloat(l);}/** * 將Byte轉換為String 或者將String轉換為Byte * * @author Administrator * *//** * 預設的字元集編碼 UTF-8 一個漢字佔三個位元組 */private static String CHAR_ENCODE = "UTF-8";/** * 設定全域的字元編碼 * * @param charEncode */public static void configCharEncode(String charEncode) {CHAR_ENCODE = charEncode;}/** * @param str * 源字串轉換成位元組數組的字串 * @return */public static byte[] StringToByte(String str) {return StringToByte(str, CHAR_ENCODE);}/** * * @param srcObj * 源位元組數群組轉換成String的位元組數組 * @return */public static String ByteToString(byte[] srcObj) {return ByteToString(srcObj, CHAR_ENCODE);}/** * UTF-8 一個漢字佔三個位元組 * * @param str * 源字串 轉換成位元組數組的字串 * @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 * 源位元組數群組轉換成String的位元組數組 * @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("\0", " ");}/** * 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);}/** * 第二種方法 *//** * 1,BYTE轉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轉SHORT * * @param b * @return */protected int byteArrayToShort(byte[] b) {return (b[0] << 8) + (b[1] & 0xFF);}/** * ,3,SHORT轉BYTE資料 * * @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) & 0xff);}return shortBuf;}/** * ,4,INT資料轉BYTE資料 * * @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,轉換long型為byte數組 * * @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,通過byte數組取到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轉換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,通過byte數組取得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轉換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,通過byte數組取得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() {//例:將字串內容轉為浮點數,再還原出原字串內容String Content = "Contiune";System.out.println("例:將字串內容轉為浮點數,再還原出原字串內容");// 1,字串轉為浮點// (1)先將字串轉為數組System.out.println("1,字串轉為浮點:(1)先將字串轉為數組Content="+ StringToByte(Content));// (2)再將數組轉化為浮點System.out.println("1,字串轉為浮點:(1)再將數組轉化為浮點Content="+ byteToDouble(StringToByte(Content)));// 2,浮點轉成字串// (1)先將浮點轉為位元組數組System.out.println("2,浮點轉成字串:(1)先將浮點轉為位元組數組Content="+ doubleToByte(byteToDouble(StringToByte(Content))));// (2)再將位元組數組轉為字串System.out.println("2,浮點轉成字串:(2)再將位元組數組轉為字串Content="+ ByteToString(doubleToByte(byteToDouble(StringToByte(Content)))));}}
列印結果如下:
例:將字串內容轉為浮點數,再還原出原字串內容
1,字串轉為浮點:(1)先將字串轉為數組Content=[[email protected]
1,字串轉為浮點:(1)再將數組轉化為浮點Content=3.949643824749462E180
2,浮點轉成字串:(1)先將浮點轉為位元組數組Content=[[email protected]
2,浮點轉成字串:(2)再將位元組數組轉為字串Content=Contiune,即還原出原字串內容。
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; border: 0px; text-indent: 0em; background: transparent;"></p>
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Java類型轉換執行個體