读书人

byte[]转化成其余数据类型

发布时间: 2012-08-28 12:37:01 作者: rapoo

byte[]转化成其他数据类型

Java与其他语言数据类型之间的转换方法实例程序

/***
?* 通信格式转换** Java和一些windows编程语言如c、c++、Delphi所写的网络程序进行通讯时,需要进行相应的转换* 高、低字
?*
?* 节之间的转换* windows的字节序为低字节开头温馨提示:您正在浏览的是 Java与其他语言数据类型之间的转换方法 的内容,您可
?*
?* 以在下面的搜索框中搜索到更多关于 Java与其他语言数据类型之间的转换方法 的文章。
?*
?* Java与其他语言数据类型之间的转换方法实例程序
?*
?* /*** 通信格式转换** Java和一些windows编程语言如c、c++、Delphi所写的网络程序进行通讯时,需要进行相应的转换* 高、低字
?*
?* 节之间的转换* windows的字节序为低字节开头* Linux,unix的字节序为高字节开头* java则无论平台变化,都是高字节开头
?*/
public class FormatTransfer {
??? /**
??? ?* * 将int转为低字节在前,高字节在后的byte数组 * @param n int * @return byte[]
??? ?*/
??? public static byte[] toLH(int n) {
??? ??? byte[] b = new byte[4];
??? ??? b[0] = (byte) (n & 0xff);
??? ??? b[1] = (byte) (n >> 8 &

??? ??? 0xff);
??? ??? b[2] = (byte) (n >> 16 & 0xff);
??? ??? b[3] = (byte) (n >> 24 & 0xff);
??? ??? return b;
??? }

??? /** * 将int转为高字节在前,低字节在后的byte数组 * @param n int * @return byte[] */
??? public static byte[] toHH(int

??? n) {
??? ??? byte[] b = new byte[4];
??? ??? b[3] = (byte) (n & 0xff);
??? ??? b[2] = (byte) (n >> 8 & 0xff);
??? ??? b[1] = (byte) (n >> 16 &

??? ??? 0xff);
??? ??? b[0] = (byte) (n >> 24 & 0xff);
??? ??? return b;
??? }

??? /** * 将short转为低字节在前,高字节在后的byte数组 * @param n short * @return byte[] */
??? public static byte[] toLH

??? (short n) {
??? ??? byte[] b = new byte[2];
??? ??? b[0] = (byte) (n & 0xff);
??? ??? b[1] = (byte) (n >> 8 & 0xff);
??? ??? return b;
??? }

??? /** * 将short转为高字节在前,低字节在后的byte数组 * @param n short * @return byte[] */
??? public static byte[] toHH

??? (short n) {
??? ??? byte[] b = new byte[2];
??? ??? b[1] = (byte) (n & 0xff);
??? ??? b[0] = (byte) (n >> 8 & 0xff);
??? ??? return b;
??? }

??? /**
??? ?* * 将将int转为高字节在前,低字节在后的byte数组 public static byte[] toHH(int number) { int
??? ?* temp = number; byte[] b = new byte[4]; for (int i = b.length - 1; i >
??? ?*
??? ?* -1; i--) { b = new Integer(temp & 0xff).byteValue(); temp = temp >> 8; }
??? ?* return b;} public static byte[] IntToByteArray(int i) { byte[] abyte0 =
??? ?* new byte[4]; abyte0[3] = (byte) (0xff & i);
??? ?*
??? ?* abyte0[2] = (byte) ((0xff00 & i) >> 8); abyte0[1] = (byte) ((0xff0000 &
??? ?* i) >> 16); abyte0[0] = (byte)
??? ?*
??? ?* ((0xff000000 & i) >> 24); return abyte0;}
??? ?*/
??? /** * 将float转为低字节在前,高字节在后的byte数组 */
??? public static byte[] toLH(float f) {

??? ??? return toLH(Float.floatToRawIntBits(f));
??? }

??? /** * 将float转为高字节在前,低字节在后的byte数组 */
??? public static byte[] toHH(float f) {
??? ??? return toHH

??? ??? (Float.floatToRawIntBits(f));
??? }

??? /** * 将String转为byte数组 */
??? public static byte[] stringToBytes(String s, int length) {
??? ??? while (s.getBytes

??? ??? ().length < length) {
??? ??? ??? s += " ";
??? ??? }
??? ??? return s.getBytes();
??? }

??? /** * 将字节数组转换为String * @param b byte[] * @return String */
??? public static String bytesToString(byte[] b) {

??? ??? StringBuffer result = new StringBuffer("");
??? ??? int length = b.length;
??? ??? for (int i = 0; i < length; i++) {

??? ??? ??? result.append((char) (b & 0xff));
??? ??? }
??? ??? return result.toString();
??? }

??? /** * 将字符串转换为byte数组 * @param s String * @return byte[] */
??? public static byte[] stringToBytes(String s) {

??? ??? return s.getBytes();
??? }

??? /** * 将高字节数组转换为int * @param b byte[] * @return int */
??? public static int hBytesToInt(byte[] b) {
??? ??? int s =

??? ??? 0;
??? ??? for (int i = 0; i < 3; i++) {
??? ??? ??? if (b >= 0) {
??? ??? ??? ??? s = s + b;
??? ??? ??? } else {
??? ??? ??? ??? s = s + 256 + b;
??? ??? ??? }
??? ??? ??? s = s *

??? ??? ??? 256;
??? ??? }
??? ??? if (b[3] >= 0) {
??? ??? ??? s = s + b[3];
??? ??? } else {
??? ??? ??? s = s + 256 + b[3];
??? ??? }
??? ??? return s;
??? }

??? /** * 将低字节数组转换为int * @param b byte[] * @return int */
??? public static int lBytesToInt(byte[] b) {
??? ??? int s =

??? ??? 0;
??? ??? for (int i = 0; i < 3; i++) {
??? ??? ??? if (b[3 - i] >= 0) {
??? ??? ??? ??? s = s + b[3 - i];
??? ??? ??? } else {
??? ??? ??? ??? s = s + 256 + b[3 - i];
??? ??? ??? }

??? ??? ??? s = s * 256;
??? ??? }
??? ??? if (b[0] >= 0) {
??? ??? ??? s = s + b[0];
??? ??? } else {
??? ??? ??? s = s + 256 + b[0];
??? ??? }
??? ??? return s;
??? }

??? /** * 高字节数组到short的转换 * @param b byte[] * @return short */
??? public static short hBytesToShort(byte[] b) {

??? ??? int s = 0;
??? ??? if (b[0] >= 0) {
??? ??? ??? s = s + b[0];
??? ??? } else {
??? ??? ??? s = s + 256 + b[0];
??? ??? }
??? ??? s = s * 256;
??? ??? if (b[1] >=

??? ??? 0) {
??? ??? ??? s = s + b[1];
??? ??? } else {
??? ??? ??? s = s + 256 + b[1];
??? ??? }
??? ??? short result = (short) s;
??? ??? return result;
??? }

??? /** * 低字节数组到short的转换 * @param b byte[] * @return short */
??? public static short lBytesToShort(byte[] b) {

??? ??? int s = 0;

??? ??? if (b[1] >= 0) {
??? ??? ??? s = s + b[1];
??? ??? } else {
??? ??? ??? s = s + 256 + b[1];
??? ??? }
??? ??? s = s * 256;
??? ??? if (b[0] >= 0) {
??? ??? ??? s =

??? ??? ??? s + b[0];
??? ??? } else {
??? ??? ??? s = s + 256 + b[0];
??? ??? }
??? ??? short result = (short) s;
??? ??? return result;
??? }

??? /** * 高字节数组转换为float * @param b byte[] * @return float */
??? public static float hBytesToFloat(byte[] b) {?

int i = 0;? Float F = new Float(0.0);? i = ((((b[0]&0xff)<<8 (b[1]&0xff))<<8) (b[2]&0xff))<<8 (b[3]&0xff);? return

F.intBitsToFloat(i);}
??? /** * 低字节数组转换为float * @param b byte[] * @return float */
??? public static float lBytesToFloat(byte[] b) {?

int i = 0;? Float F = new Float(0.0);? i = ((((b[3]&0xff)<<8 (b[2]&0xff))<<8) (b[1]&0xff))<<8 (b[0]&0xff);? return

F.intBitsToFloat(i);}
??? /** * 将byte数组中的元素倒序排列 */
??? public static byte[] bytesReverseOrder(byte[] b) {
??? ??? int length = b.length;

??? ??? byte[] result = new byte[length];
??? ??? for (int i = 0; i < length; i++) {
??? ??? ??? result[length - i - 1] = b;
??? ??? }
??? ??? return result;
??? }

??? /** * 打印byte数组 */
??? public static void printBytes(byte[] bb) {
??? ??? int length = bb.length;
??? ??? for (int i = 0; i < length;

??? ??? i++) {
??? ??? ??? System.out.print(bb + " ");
??? ??? }
??? ??? System.out.println("");
??? }

??? public static void logBytes(byte[] bb) {
??? ??? int length = bb.length;
??? ??? String out = "";
??? ??? for (int i = 0; i < length; i++) {

??? ??? ??? out = out + bb + " ";
??? ??? }
??? }

??? /** * 将int类型的值转换为字节序颠倒过来对应的int值 * @param i int * @return int */
??? public static int reverseInt

??? (int i) {
??? ??? int result = FormatTransfer.hBytesToInt(FormatTransfer.toLH(i));
??? ??? return result;
??? }

??? /** * 将short类型的值转换为字节序颠倒过来对应的short值 * @param s short * @return short */
??? public static short

??? reverseShort(short s) {
??? ??? short result = FormatTransfer.hBytesToShort(FormatTransfer.toLH(s));
??? ??? return result;
??? }

??? /** * 将float类型的值转换为字节序颠倒过来对应的float值 * @param f float * @return float */
??? public static float

??? reverseFloat(float f) {
??? ??? float result = FormatTransfer.hBytesToFloat(FormatTransfer.toLH(f));
??? ??? return result;
??? }
}
























在剖析该问题前请看如下代码
public static String bytes2HexString(byte[] b) {
? String ret = "";
? for (int i = 0; i < b.length; i++) {
?? String hex = Integer.toHexString(b[ i ] & 0xFF);
?? if (hex.length() == 1) {
??? hex = '0' + hex;
?? }
?? ret += hex.toUpperCase();
? }
? return ret;
}
上面是将byte[]转化十六进制的字符串,注意这里b[ i ] & 0xFF将一个byte和 0xFF进行了与运算,然后使用Integer.toHexString取得了十六进制字符串,可以看出
b[ i ] & 0xFF运算后得出的仍然是个int,那么为何要和 0xFF进行与运算呢?直接 Integer.toHexString(b[ i ]);,将byte强转为int不行吗?答案是不行的.

其原因在于:
1.byte的大小为8bits而int的大小为32bits
2.java的二进制采用的是补码形式

在这里先温习下计算机基础理论

byte是一个字节保存的,有8个位,即8个0、1。
8位的第一个位是符号位,
也就是说0000 0001代表的是数字1
1000 0000代表的就是-1
所以正数最大位0111 1111,也就是数字127
负数最大为1111 1111,也就是数字-128

上面说的是二进制原码,但是在java中采用的是补码的形式,下面介绍下什么是补码

1、反码:
??????? 一个数如果是正,则它的反码与原码相同;
??????? 一个数如果是负,则符号位为1,其余各位是对原码取反;

2、补码:利用溢出,我们可以将减法变成加法
?????? 对于十进制数,从9得到5可用减法:
?????? 9-4=5??? 因为4+6=10,我们可以将6作为4的补数
?????? 改写为加法:
?????? 9+6=15(去掉高位1,也就是减10)得到5.

?????? 对于十六进制数,从c到5可用减法:
?????? c-7=5??? 因为7+9=16 将9作为7的补数
?????? 改写为加法:
?????? c+9=15(去掉高位1,也就是减16)得到5.

??? 在计算机中,如果我们用1个字节表示一个数,一个字节有8位,超过8位就进1,在内存中情况为(100000000),进位1被丢弃。

??? ⑴一个数为正,则它的原码、反码、补码相同
??? ⑵一个数为负,刚符号位为1,其余各位是对原码取反,然后整个数加1
???
- 1的原码为??????????????? 10000001
- 1的反码为??????????????? 11111110
?????????????????????????????????????????????????? + 1
- 1的补码为??????????????? 11111111

0的原码为???????????????? 00000000
0的反码为???????????????? 11111111(正零和负零的反码相同)
????????????????????????????????????????? +1
0的补码为?????????????? 100000000(舍掉打头的1,正零和负零的补码相同)

Integer.toHexString的参数是int,如果不进行&0xff,那么当一个byte会转换成int时,由于int是32位,而byte只有8位这时会进行补位,
例如补码11111111的十进制数为-1转换为int时变为11111111111111111111111111111111好多1啊,呵呵!即0xffffffff但是这个数是不对的,这种补位就会造成误差。
和0xff相与后,高24比特就会被清0了,结果就对了。

----
Java中的一个byte,其范围是-128~127的,而Integer.toHexString的参数本来是int,如果不进行&0xff,那么当一个byte会转换成int时,对于负数,会做位扩展,举例来说,一个byte的-1(即0xff),会被转换成int的-1(即0xffffffff),那么转化出的结果就不是我们想要的了。

而0xff默认是整形,所以,一个byte跟0xff相与会先将那个byte转化成整形运算,这样,结果中的高的24个比特就总会被清0,于是结果总是我们想要的。

读书人网 >编程

热点推荐