Java 中常用的类型转换

来源:互联网 发布:陈怡馨被迷 知乎 编辑:程序博客网 时间:2024/06/06 02:23

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

首先复习一下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 blocke.printStackTrace();}    return s;}
示例为String s = "hello";







0 0
原创粉丝点击