Java 中常用的类型转换

基础不扎实,一遇到类型转换的问题就得找度娘,所以利用这段时间,自己总结一下(重学一下大哭)。

首先复习一下Java中的类型:1byte = 8bit;一个short占2byte;一个long占8byte;一个int占4byte;一个char占2byte;一个float占4byte;一个double占8byte

Java中常用的主要类型有int、String、float、double、char、byte等。

1.int与String类型的相互转换,float、double和int类似,转换方法相似。

int---->String

int i;
String s = String.valueOf(i);
String s = Integer.toString(i);
String s = i + "";
String--->int

String s;
int i = Integer.parseInt(s);
int i = Integer.valueOf(s).intValue();

String--->float、double

String s = "123";
float fs = Float.parseFloat(s);
float sf = Float.valueOf(s).floatValue();
double ds = Double.parseDouble(s);
double sd = Double.valueOf(s).doubleValue();

2.byte[]和int类型的相互转换

int--->byte[]

public static byte[] intToBytes2(int value)   
{   
    byte[] src = new byte[4];  
    src[0] = (byte) ((value>>24) & 0xFF);  
    src[1] = (byte) ((value>>16)& 0xFF);  
    src[2] = (byte) ((value>>8)&0xFF);    
    src[3] = (byte) (value & 0xFF);       
    return src;  
}
这里需要注意int数据是由高位到低位的排序。

byte[]--->int

public static int byteToInt2(byte[] src, int offset) {  
	int value;    
	value = (int) ( ((src[offset] & 0xFF)<<24)  
	      |((src[offset+1] & 0xFF)<<16)  
	      |((src[offset+2] & 0xFF)<<8)  
	      |(src[offset+3] & 0xFF));  
	return value;  
}
其中,offset为byte[]开始位置。下面的实例为:int a = 10 进行的转换


3.byte[]和float类型的相互转换

float--->byte[]

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;       
}  
byte[]--->float

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[]转float,前者使用较少,下面示例效果为float a = 10.0f 


3.double和byte[]类型的相互转换
double--->byte[]

public static byte[] double2Bytes(double d) {  
      long value = Double.doubleToRawLongBits(d);  
      byte[] byteRet = new byte[8];  
      for (int i = 0; i < 8; i++) {  
          byteRet[i] = (byte) ((value >> 8 * i) & 0xff);  
      }  
      return byteRet;  
}
byte[]--->double

public static double bytes2Double(byte[] arr) {  
      long value = 0;  
      for (int i = 0; i < 8; i++) {  
          value |= ((long) (arr[i] & 0xff)) << (8 * i);  
      }  
      return Double.longBitsToDouble(value);  
}
示例为double d = 10.0 

4.byte[]和String类型的相互转换

String--->byte[]

public static byte[] stringTobyte(String s){
    byte[] b = s.getBytes();
    return b;
}
byte[]--->String

public static String byteTostring(byte[] b){
    String s = null;
	try {
		s = new String(b,"UTF-8");
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    return s;
}
示例为String s = "hello";







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值