1、 & b5 = (byte) (n 16 & b4 = (byte) (n 24 & b3 = (byte) (n 32 & b2 = (byte) (n 40 & b1 = (byte) (n 48 & b0 = (byte) (n 56 & return b; public static void longToBytes( long n, byte array, int offset ) array7+offset = (byte) (n & array6+offset = (byte) (n 8 & array5+offset = (byte) (n array4+offset = (b
2、yte) (n array3+offset = (byte) (n array2+offset = (byte) (n array1+offset = (byte) (n array0+offset = (byte) (n public static long bytesToLong( byte array ) return (long) array 0 & 0xff) public static int bytesToInt(byte b) return b3 & 0xff | (b2 & 8 | (b1 & 16 | (b0 & 24; public static int bytesToI
3、nt(byte b, int offset) boffset+3 & | (boffset+2 & | (boffset+1 & | (boffset & public static byte uintToBytes( long n ) public static void uintToBytes( long n, byte array, int offset ) array3+offset = (byte) (n ); arrayoffset = (byte) (n public static long bytesToUint(byte array) return (long) (array
4、3 & 0xff) | (long) (array2 & 0xff) 8) & public static void shortToBytes(short n, byte array, int offset ) arrayoffset+1 = (byte) ( n arrayoffset = (byte) (n public static short bytesToShort(byte b) return (short)( b1 & 0xff |(b0 & 8 ); public static short bytesToShort(byte b, int offset) return (sho
5、rt)( boffset+1 & |(boffset public static byte ushortToBytes(int n) public static void ushortToBytes(int n, byte array, int offset ) arrayoffset = (byte) (n public static int bytesToUshort(byte b) b1 & 8; public static int bytesToUshort(byte b, int offset) boffset+1 & | (boffset public static byte ub
6、yteToBytes( int n ) byte b = new byte1; b0 = (byte) (n & public static void ubyteToBytes( int n, byte array, int offset ) array0 = (byte) (n & public static int bytesToUbyte( byte array ) return array0 & 0xff; public static int bytesToUbyte( byte array, int offset ) return arrayoffset & / char 类型、 f
7、loat、double 类型和 byte 数组之间的转换关系还需继续研究实现。测试程序如下:public class ByteConvertTest public static String byte2Hex(byte buf) StringBuffer strbuf = new StringBuffer(); strbuf.append(); for (byte b : buf) if (b = 0) 00 else if (b = -1) FF else String str = Integer.toHexString(b).toUpperCase(); / sb.append(a); i
8、f (str.length() = 8) str = str.substring(6, 8); else if (str.length() 2) str = 0 + str; strbuf.append(str); return strbuf.toString(); 0xff) ); static void testShortConvert() System.out.println(= short convert =byte2Hex(shortToBytes(short)0x11f2)+byte2Hex(shortToBytes(short)0x11f2); System.out.print(println 0x11f2: System.out.println(short)0x11f2);byte2Hex(shortToBytes(short)0xf1f2)+byte2Hex(shortToBytes(short)0xf1f2);print
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1