牛骨文教育服务平台(让学习变的简单)
博文笔记

Java类型转换(int - byte[],float - byte[])

创建时间:2013-12-15 投稿人: 浏览次数:3062
 

Java类型转换(int - byte[],float - byte[])

分类: Java AND Android2012-08-20 11:22 2360人阅读 评论(1) 收藏 举报 bytefloatjavaintegerstream语言

目录(?)[+]

有符号字节转成无符号字节 java int 转成 byte

[java] view plaincopy
  1. int unsignedByte = signedByte >= 0 ? signedByte :256 + signedByte;  
  2. int byteValue;  
  3. int temp = intValue % 256;  
  4. f ( intValue < 0) {  
  5.        byteValue = temp < -128 ?256 + temp : temp;  
  6. }  
  7. else {  
  8.        byteValue = temp > 127 ? temp -256 : temp;  
  9. }   
 

float -> byte[]

先用 Float.floatToIntBits(f)转换成int
再通过如下方法转成byte []

 

int -> byte[]

[java] view plaincopy
  1. /** 
  2.   * 将int类型的数据转换为byte数组 
  3.   * 原理:将int数据中的四个byte取出,分别存储 
  4.   * @param n int数据 
  5.   * @return 生成的byte数组 
  6.   */  
  7. public static byte[] intToBytes2(int n){  
  8.     byte[] b = new byte[4];  
  9.     for(int i = 0;i < 4;i++){  
  10.         b[i] = (byte)(n >> (24 - i * 8));   
  11.     }  
  12.     return b;  
  13. }  
  14.   
  15. byte[] -> int  
  16. /** 
  17.   * 将byte数组转换为int数据 
  18.   * @param b 字节数组 
  19.   * @return 生成的int数据 
  20.   */  
  21.   public static int byteToInt2(byte[] b){  
  22.       return (((int)b[0]) << 24) + (((int)b[1]) << 16) + (((int)b[2]) << 8) + b[3];  
  23.   }  
  24. }  

byte[] -> float

[java] view plaincopy
  1. public static float getFloat(byte[] b) {  
  2.     // 4 bytes  
  3.     int accum = 0;  
  4.     for ( int shiftBy = 0; shiftBy < 4; shiftBy++ ) {  
  5.             accum |= (b[shiftBy] & 0xff) << shiftBy * 8;  
  6.     }  
  7.     return Float.intBitsToFloat(accum);  
  8. }  

老物,,在通信的时候会用到

http://xuliduo.iteye.com/blog/1126957

 

[java] view plaincopy
  1. 1.package com.util;    
  2. 2.    
  3. 3./**  
  4. 4. *   
  5. 5. * <ul>  
  6. 6. * <li>文件名称: com.born.util.ByteUtil.java</li>  
  7. 7. * <li>文件描述: byte转换工具</li>  
  8. 8. * <li>版权所有: 版权所有(C)2001-2006</li>  
  9. 9. * <li>公 司: bran</li>  
  10. 10. * <li>内容摘要:</li>  
  11. 11. * <li>其他说明:</li>  
  12. 12. * <li>完成日期:2011-7-18</li>  
  13. 13. * <li>修改记录0:无</li>  
  14. 14. * </ul>  
  15. 15. *   
  16. 16. * @version 1.0  
  17. 17. * @author 许力多  
  18. 18. */    
  19. 19.public class ByteUtil {    
  20. 20.    /**  
  21. 21.     * 转换short为byte  
  22. 22.     *   
  23. 23.     * @param b  
  24. 24.     * @param s  
  25. 25.     *            需要转换的short  
  26. 26.     * @param index  
  27. 27.  
  28. 28.     */    
  29. 29.    public static void putShort(byte b[], short s, int index) {    
  30. 30.        b[index + 1] = (byte) (s >> 8);    
  31. 31.        b[index + 0] = (byte) (s >> 0);    
  32. 32.    }    
  33. 33.    
  34. 34.    /**  
  35. 35.     * 通过byte数组取到short  
  36. 36.     *   
  37. 37.     * @param b  
  38. 38.     * @param index  
  39. 39.     *            第几位开始取  
  40. 40.     * @return  
  41. 41.     */    
  42. 42.    public static short getShort(byte[] b, int index) {    
  43. 43.        return (short) (((b[index + 1] << 8) | b[index + 0] & 0xff));    
  44. 44.    }    
  45. 45.    
  46. 46.    /**  
  47. 47.     * 转换int为byte数组  
  48. 48.     *   
  49. 49.     * @param bb  
  50. 50.     * @param x  
  51. 51.     * @param index  
  52. 52.     */    
  53. 53.    public static void putInt(byte[] bb, int x, int index) {    
  54. 54.        bb[index + 3] = (byte) (x >> 24);    
  55. 55.        bb[index + 2] = (byte) (x >> 16);    
  56. 56.        bb[index + 1] = (byte) (x >> 8);    
  57. 57.        bb[index + 0] = (byte) (x >> 0);    
  58. 58.    }    
  59. 59.    
  60. 60.    /**  
  61. 61.     * 通过byte数组取到int  
  62. 62.     *   
  63. 63.     * @param bb  
  64. 64.     * @param index  
  65. 65.     *            第几位开始  
  66. 66.     * @return  
  67. 67.     */    
  68. 68.    public static int getInt(byte[] bb, int index) {    
  69. 69.        return (int) ((((bb[index + 3] & 0xff) << 24)    
  70. 70.                | ((bb[index + 2] & 0xff) << 16)    
  71. 71.                | ((bb[index + 1] & 0xff) << 8) | ((bb[index + 0] & 0xff) << 0)));    
  72. 72.    }    
  73. 73.    
  74. 74.    /**  
  75. 75.     * 转换long型为byte数组  
  76. 76.     *   
  77. 77.     * @param bb  
  78. 78.     * @param x  
  79. 79.     * @param index  
  80. 80.     */    
  81. 81.    public static void putLong(byte[] bb, long x, int index) {    
  82. 82.        bb[index + 7] = (byte) (x >> 56);    
  83. 83.        bb[index + 6] = (byte) (x >> 48);    
  84. 84.        bb[index + 5] = (byte) (x >> 40);    
  85. 85.        bb[index + 4] = (byte) (x >> 32);    
  86. 86.        bb[index + 3] = (byte) (x >> 24);    
  87. 87.        bb[index + 2] = (byte) (x >> 16);    
  88. 88.        bb[index + 1] = (byte) (x >> 8);    
  89. 89.        bb[index + 0] = (byte) (x >> 0);    
  90. 90.    }    
  91. 91.    
  92. 92.    /**  
  93. 93.     * 通过byte数组取到long  
  94. 94.     *   
  95. 95.     * @param bb  
  96. 96.     * @param index  
  97. 97.     * @return  
  98. 98.     */    
  99. 99.    public static long getLong(byte[] bb, int index) {    
  100. 100.        return ((((long) bb[index + 7] & 0xff) << 56)    
  101. 101.                | (((long) bb[index + 6] & 0xff) << 48)    
  102. 102.                | (((long) bb[index + 5] & 0xff) << 40)    
  103. 103.                | (((long) bb[index + 4] & 0xff) << 32)    
  104. 104.                | (((long) bb[index + 3] & 0xff) << 24)    
  105. 105.                | (((long) bb[index + 2] & 0xff) << 16)    
  106. 106.                | (((long) bb[index + 1] & 0xff) << 8) | (((long) bb[index + 0] & 0xff) << 0));    
  107. 107.    }    
  108. 108.    
  109. 109.    /**  
  110. 110.     * 字符到字节转换  
  111. 111.     *   
  112. 112.     * @param ch  
  113. 113.     * @return  
  114. 114.     */    
  115. 115.    public static void putChar(byte[] bb, char ch, int index) {    
  116. 116.        int temp = (int) ch;    
  117. 117.        // byte[] b = new byte[2];    
  118. 118.        for (int i = 0; i < 2; i ++ ) {    
  119. 119.            bb[index + i] = new Integer(temp & 0xff).byteValue(); // 将最高位保存在最低位    
  120. 120.            temp = temp >> 8; // 向右移8位    
  121. 121.        }    
  122. 122.    }    
  123. 123.    
  124. 124.    /**  
  125. 125.     * 字节到字符转换  
  126. 126.     *   
  127. 127.     * @param b  
  128. 128.     * @return  
  129. 129.     */    
  130. 130.    public static char getChar(byte[] b, int index) {    
  131. 131.        int s = 0;    
  132. 132.        if (b[index + 1] > 0)    
  133. 133.            s += b[index + 1];    
  134. 134.        else    
  135. 135.            s += 256 + b[index + 0];    
  136. 136.        s *= 256;    
  137. 137.        if (b[index + 0] > 0)    
  138. 138.            s += b[index + 1];    
  139. 139.        else    
  140. 140.            s += 256 + b[index + 0];    
  141. 141.        char ch = (char) s;    
  142. 142.        return ch;    
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。