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