zoukankan      html  css  js  c++  java
  • Java类型转换实例 分类: Java 2015-08-04 16:13 10人阅读 评论(0) 收藏

    例:将字符串内容转为浮点数,再还原出原字符串内容
    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];
    			else
    
    				s = s + 256 + b[i];
    			s = s * 256;
    		}
    		if (b[3] >= 0) // 最后一个之所以不乘,是因为可能会溢出
    			s = s + b[3];
    		else
    			s = 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];
    		else
    			s += 256 + b[0];
    		s *= 256;
    		if (b[1] > 0)
    			s += b[1];
    		else
    			s += 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("", " ");
    	}
    
    	/**
    	 * 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=[B@659e0bfd
    1,字符串转为浮点:(1)再将数组转化为浮点Content=3.949643824749462E180
    2,浮点转成字符串:(1)先将浮点转为字节数组Content=[B@2a139a55
    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>

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Gradle gitignore Gradle 模式 上传SVN 要忽略的文件
    加速Android Studio/Gradle构建
    JAVA unicode转换成中文
    进程与线程
    Flask快速入门
    tensorflow入门指南
    BP神经网络与Python实现
    文档数据库MongoDB
    Python虚拟环境virtualenv
    Python爬虫框架Scrapy
  • 原文地址:https://www.cnblogs.com/xieping/p/4714148.html
Copyright © 2011-2022 走看看