---
title: "java类型转换"
author: "zhengxuezhou"
date: "2011-05-20T17:13:53Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2011-05-20T17:13:53Z"
id: 1051926
url: "https://fire11.iteye.com/blog/1051926"
---

string和int之间的转换?

字符串转换成数据

Java代码 [![复制代码](http://whotmmd.iteye.com/images/icon_copy.gif)](http://whotmmd.iteye.com/blog/417576 "复制代码") [![收藏代码](http://whotmmd.iteye.com/images/icon_star.png)![](http://whotmmd.iteye.com/images/spinner.gif)]()

1. String MyNumber ="1234";
2. **int** MyInt = Integer.parseInt(MyNumber);

```
String MyNumber ="1234"; 
int MyInt = Integer.parseInt(MyNumber); 
```

字符串转换成byte, short, int, float, double, long等数据类型，可以分别参考Byte, Short, Integer, Float, Double, Long类的parseXXX 方法。

Java代码 [![复制代码](http://whotmmd.iteye.com/images/icon_copy.gif)](http://whotmmd.iteye.com/blog/417576 "复制代码") [![收藏代码](http://whotmmd.iteye.com/images/icon_star.png)![](http://whotmmd.iteye.com/images/spinner.gif)]()

1. a1=Integer.parseInt(s1);
2. s1=Integer.toString(a1);

```
a1=Integer.parseInt(s1); 
s1=Integer.toString(a1); 
```

数据转换成字符串

Java代码 [![复制代码](http://whotmmd.iteye.com/images/icon_copy.gif)](http://whotmmd.iteye.com/blog/417576 "复制代码") [![收藏代码](http://whotmmd.iteye.com/images/icon_star.png)![](http://whotmmd.iteye.com/images/spinner.gif)]()

1. **int** MyInt = 1234;
2. String MyString = "" + MyInt;

```
int MyInt = 1234; 
String MyString = "" + MyInt; 
```

其它数据类型可以利用同样的方法转换成字符串。

十进制到其他进制的转换  
十进制整数转换成二进制整数，返回结果是一个字符串：  
Integer.toBinaryString(int i);  
Integer和Long提供了toBinaryString, toHexString和toOctalString方法，可以方便的将数据转换成二进制、十六进制和八进制字符串。功能更加强大的是其toString(int/long i, int radix)方法，可以将一个十进制数转换成任意进制的字符串形式。  
byte, short, float和double等数据类型，可以利用Integer或者是Long的toBinaryString, toHexString, to OctalString和toString方法转换成其他进制的字符串形式。

其它进制到十进制的转换  
五进制字符串14414转换成十进制整数，结果是1234：  
System.out.println(Integer.valueOf("14414", 5);  
Integer和Long提供的valueOf(String source, int radix)方法，可以  
将任意进制的字符串转换成十进制数据。

把String类型转换成16进制的整数

Java代码 [![复制代码](http://whotmmd.iteye.com/images/icon_copy.gif)](http://whotmmd.iteye.com/blog/417576 "复制代码") [![收藏代码](http://whotmmd.iteye.com/images/icon_star.png)![](http://whotmmd.iteye.com/images/spinner.gif)]()

1. **public** **static** **void** main(String args\[]){
2. String x = "0x300C8";
3. **int** y = Integer.decode(x).intvalue();
4. System.out.println(y);
5. }

```
public static void main(String args[]){
String x = "0x300C8";
int y = Integer.decode(x).intvalue();
System.out.println(y);
}
```

int、char、double与byte相互转换的程序  
整数到字节数组的转换

Java代码 [![复制代码](http://whotmmd.iteye.com/images/icon_copy.gif)](http://whotmmd.iteye.com/blog/417576 "复制代码") [![收藏代码](http://whotmmd.iteye.com/images/icon_star.png)![](http://whotmmd.iteye.com/images/spinner.gif)]()

1. **public** **static** **byte**\[] intToByte(**int** number) {
2. **int** temp = number;
3. **byte**\[] b=**new** **byte**\[4];
4. **for** (**int** i=b.length-1;i&gt;-1;i--){
5. b\[i] = **new** Integer(temp&0xff).byteValue();      //将最高位保存在最低位
6. temp = temp &gt;&gt; 8;       //向右移8位
7. }
8. **return** b;
9. }

```
  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;
  }
```

  字节数组到整数的转换  
 

Java代码 [![复制代码](http://whotmmd.iteye.com/images/icon_copy.gif)](http://whotmmd.iteye.com/blog/417576 "复制代码") [![收藏代码](http://whotmmd.iteye.com/images/icon_star.png)![](http://whotmmd.iteye.com/images/spinner.gif)]()

01. **public** **static** **int** byteToInt(**byte**\[] b) {
02. **int** s = 0;
03. **for** (**int** i = 0; i &lt; 3; i++) {
04. **if** (b\[i] &gt;= 0)
05. s = s + b\[i];
06. **else**
07. s = s + 256 + b\[i];
08. s = s * 256;
09. }
10. **if** (b\[3] &gt;= 0)       //最后一个之所以不乘，是因为可能会溢出
11. s = s + b\[3];
12. **else**
13. s = s + 256 + b\[3];
14. **return** s;
15. }

```
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;
  }
```

短整数与字节数组之间的相互转换  
short与int之间的区别在于short是两个字节的，而int是四个字节的。因此，只需要将5 与6 中的范例程序小做改动，即可实现短整数与字节数组之间的相互转换。

  字符到字节转换  
 

Java代码 [![复制代码](http://whotmmd.iteye.com/images/icon_copy.gif)](http://whotmmd.iteye.com/blog/417576 "复制代码") [![收藏代码](http://whotmmd.iteye.com/images/icon_star.png)![](http://whotmmd.iteye.com/images/spinner.gif)]()

1. **public** **static** **byte**\[] charToByte(**char** ch){
2. **int** temp=(**int**)ch;
3. **byte**\[] b=**new** **byte**\[2];
4. **for** (**int** i=b.length-1;i&gt;-1;i--){
5. b\[i] = **new** Integer(temp&0xff).bytevalue();      //将最高位保存在最低位
6. temp = temp &gt;&gt; 8;       //向右移8位
7. }
8. **return** b;
9. }

```
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;
  }
```

  //字节到字符转换  
 

Java代码 [![复制代码](http://whotmmd.iteye.com/images/icon_copy.gif)](http://whotmmd.iteye.com/blog/417576 "复制代码") [![收藏代码](http://whotmmd.iteye.com/images/icon_star.png)![](http://whotmmd.iteye.com/images/spinner.gif)]()

01. **public** **static** **char** byteToChar(**byte**\[] b){
02. **int** s=0;
03. **if**(b\[0]&gt;0)
04. s+=b\[0];
05. **else**
06. s+=256+b\[0];
07. s\*=256;
08. **if**(b\[1]&gt;0)
09. s+=b\[1];
10. **else**
11. s+=256+b\[1];
12. **char** ch=(**char**)s;
13. **return** ch;
14. }

```
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;
  }
```

  浮点到字节转换  
 

Java代码 [![复制代码](http://whotmmd.iteye.com/images/icon_copy.gif)](http://whotmmd.iteye.com/blog/417576 "复制代码") [![收藏代码](http://whotmmd.iteye.com/images/icon_star.png)![](http://whotmmd.iteye.com/images/spinner.gif)]()

1. **public** **static** **byte**\[] doubleToByte(**double** d){
2. **byte**\[] b=**new** **byte**\[8];
3. **long** l=Double.doubleToLongBits(d);
4. **for**(**int** i=0;i&lt;b.length;i++){
5. b\[i]=**new** Long(l).bytevalue();
6. l=l&gt;&gt;8;
7. }
8. **return** b;
9. }

```
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;
  }
```

  字节到浮点转换  
 

Java代码 [![复制代码](http://whotmmd.iteye.com/images/icon_copy.gif)](http://whotmmd.iteye.com/blog/417576 "复制代码") [![收藏代码](http://whotmmd.iteye.com/images/icon_star.png)![](http://whotmmd.iteye.com/images/spinner.gif)]()

01. **public** **static** **double** byteToDouble(**byte**\[] b){
02. **long** l;
03. l=b\[0];
04. l&=0xff;
05. l|=((**long**)b\[1]&lt;&lt;8);
06. l&=0xffff;
07. l|=((**long**)b\[2]&lt;&lt;16);
08. l&=0xffffff;
09. l|=((**long**)b\[3]&lt;&lt;24);
10. l&=0xffffffffl;
11. l|=((**long**)b\[4]&lt;&lt;32);
12. l&=0xffffffffffl;
13. l|=((**long**)b\[5]&lt;&lt;40);
14. l&=0xffffffffffffl;
15. l|=((**long**)b\[6]&lt;&lt;48);
16. l|=((**long**)b\[7]&lt;&lt;56);
17. **return** Double.longBitsToDouble(l);
18. }

```
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);
  }
```

int与byte array之间的转换程序  
在通讯中经常需要将数值转换成字节流，或者是将字节流转换成数值。下面  
提供的程序可以进行int和byte array之间的转换。

Java代码 [![复制代码](http://whotmmd.iteye.com/images/icon_copy.gif)](http://whotmmd.iteye.com/blog/417576 "复制代码") [![收藏代码](http://whotmmd.iteye.com/images/icon_star.png)![](http://whotmmd.iteye.com/images/spinner.gif)]()

01. /\**
02. \*
03. * IntConverter
04. \*
05. * This class provides methods to convert int into byte array and
06. * byte array back into int.
07. \*
08. \*/
09. **public** **class** IntConverter
10. {
11. /\**
12. \*
13. * Method converting int into byte array.
14. \*
15. * @param number The int value to be converted.
16. \*
17. \*/
18. **public** **static** **byte**\[] toByteArray(**int** number)
19. {
20. **int** temp = number;
21. **byte**\[] b=**new** **byte**\[4];
22. **for** (**int** i = b.length - 1; i &gt; -1; i--)
23. {
24. b\[i] = **new** Integer(temp & 0xff).bytevalue();
25. temp = temp &gt;&gt; 8;
26. }
27. **return** b;
28. }
29. /\**
30. \*
31. * Method converting byte array into int.
32. \*
33. * @param The byte array to be converted.
34. \*
35. \*/
36. **public** **static** **int** toInteger(**byte**\[] b)
37. {
38. **int** s = 0;
39. **for** (**int** i = 0; i &lt; 3; i++)
40. {
41. **if** (b\[i] &gt; 0)
42. s = s + b\[i];
43. **else**
44. s = s + 256 + b\[i];
45. s = s * 256;
46. }
47. **if** (b\[3] &gt; 0)
48. s = s + b\[3];
49. **else**
50. s = s + 256 + b\[3];
51. **return** s;
52. }
53. // Testing program.
54. **public** **static** **void** main(String\[] args)
55. {
56. IntConverter abc = **new** IntConverter();
57. **int** s = -1121115678;
58. **byte**\[] b = abc.toByteArray(s);
59. **for** (**int** i = 0; i &lt;= 3; i++)
60. System.out.println(b\[i]);
61. s = abc.toInteger(b);
62. System.out.println(s);
63. }
64. }

```
/**
 *
 * IntConverter
 *
 * This class provides methods to convert int into byte array and
 * byte array back into int.
 *
 */
public class IntConverter
{
        /**
         *
         * Method converting int into byte array.
         *
         * @param number The int value to be converted.
         *
         */
        public static byte[] toByteArray(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;
            }
            return b;
        }
 
        /**
         *
         * Method converting byte array into int.
         *
         * @param The byte array to be converted.
         *
         */
        public static int toInteger(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;
        }
 
    // Testing program.
    public static void main(String[] args)
    {
        IntConverter abc = new IntConverter();
        int s = -1121115678;
        byte[] b = abc.toByteArray(s);
        for (int i = 0; i <= 3; i++)
                System.out.println(b[i]);
        s = abc.toInteger(b);
        System.out.println(s);
    }
}
```

字节数组到整数的转换

Java代码 [![复制代码](http://whotmmd.iteye.com/images/icon_copy.gif)](http://whotmmd.iteye.com/blog/417576 "复制代码") [![收藏代码](http://whotmmd.iteye.com/images/icon_star.png)![](http://whotmmd.iteye.com/images/spinner.gif)]()

01. **public** **static** **int** byteToint(**byte**\[] convertByteValue)
02. { **byte**\[] YY=**new** **byte**\[4];
03. YY=convertByteValue;
04. **int** ee, ff, gg, hh;
05. ee = YY\[3] & 0x000000ff;
06. //System.out.println("ee: " +ee);
07. ff = (YY\[2]&lt;&lt;8) & 0x0000ff00;
08. //System.out.println("ff: " +ff);
09. gg = (YY\[1]&lt;&lt;16) & 0x00ff0000;
10. // System.out.println("gg: " +gg);
11. hh = YY\[0]&lt;&lt;24;
12. // System.out.println("hh: "+hh);
13. **int** jj = ee + ff + gg + hh;
14. // System.out.println("jj: "+jj);
15. **return** jj;
16. }

```
public static int byteToint(byte[] convertByteValue)
     { byte[] YY=new byte[4];
      YY=convertByteValue;
      int ee, ff, gg, hh;
      ee = YY[3] & 0x000000ff;
      //System.out.println("ee: " +ee);
      ff = (YY[2]<<8) & 0x0000ff00;
      //System.out.println("ff: " +ff);
      gg = (YY[1]<<16) & 0x00ff0000;
      // System.out.println("gg: " +gg);
      hh = YY[0]<<24;
      // System.out.println("hh: "+hh);
      int jj = ee + ff + gg + hh;
      // System.out.println("jj: "+jj);
      return jj;
      }
```

    整数到字节数组的转换

  

Java代码 [![复制代码](http://whotmmd.iteye.com/images/icon_copy.gif)](http://whotmmd.iteye.com/blog/417576 "复制代码") [![收藏代码](http://whotmmd.iteye.com/images/icon_star.png)![](http://whotmmd.iteye.com/images/spinner.gif)]()

01. **public** **byte**\[] intTobyte(**int** convertIntValue)
02. { **int** Y;
03. Y = 1321432453;
04. **byte** YY\[] = **new** **byte**\[4];
05. Integer aa = **new** Integer(Y);
06. YY\[3] = aa.byteValue();
07. Integer bb = **new** Integer(Y&gt;&gt;&gt;8);
08. YY\[2] = bb.byteValue();
09. Integer cc = **new** Integer(Y&gt;&gt;&gt;16);
10. YY\[1] = cc.byteValue();
11. Integer dd = **new** Integer(Y&gt;&gt;&gt;24);
12. YY\[0] = dd.byteValue();
13. **return** YY;
14. }