精华内容
下载资源
问答
  • 网络字节序转换

    2012-10-18 16:14:31
    工作项目需要在java和c/c++...如果发送方和接收方都是同种语言,则一般只涉及到字节序调整。而对于java和c/c++的通信,则情况就要复杂一些,主要是因为java中没有unsigned类型,并且java和c在某些数据类型上的长...
    工作项目需要在java和c/c++之间进行socket通信,socket通信是以字节流或者字节包进行的,socket发送方须将数据转换为字节流或者字节包,而接收方则将字节流和字节包再转换回相应的数据类型。如果发送方和接收方都是同种语言,则一般只涉及到字节序的调整。而对于java和c/c++的通信,则情况就要复杂一些,主要是因为java中没有unsigned类型,并且java和c在某些数据类型上的长度不一致。
      本文就是针对这种情况,整理了java数据类型和网络字节流或字节包(相当于java的byte数组)之间转换方法。实际上网上这方面的资料不少,但往往不全,甚至有些有错误,于是就花了点时间对java整型数和网络字节序的byte[]之间转换的各种情况做了一些验证和整理。整理出来的函数如下:

    public class ByteConvert {
    // 以下 是整型数 和 网络字节序的 byte[] 数组之间的转换
    public static byte[] longToBytes(long n) {
    byte[] b = new byte[8];
    b[7] = (byte) (n & 0xff);
    b[6] = (byte) (n >> 8 & 0xff);
    b[5] = (byte) (n >> 16 & 0xff);
    b[4] = (byte) (n >> 24 & 0xff);
    b[3] = (byte) (n >> 32 & 0xff);
    b[2] = (byte) (n >> 40 & 0xff);
    b[1] = (byte) (n >> 48 & 0xff);
    b[0] = (byte) (n >> 56 & 0xff);
    return b;
    }

    public static void longToBytes( long n, byte[] array, int offset ){
    array[7+offset] = (byte) (n & 0xff);
    array[6+offset] = (byte) (n >> 8 & 0xff);
    array[5+offset] = (byte) (n >> 16 & 0xff);
    array[4+offset] = (byte) (n >> 24 & 0xff);
    array[3+offset] = (byte) (n >> 32 & 0xff);
    array[2+offset] = (byte) (n >> 40 & 0xff);
    array[1+offset] = (byte) (n >> 48 & 0xff);
    array[0+offset] = (byte) (n >> 56 & 0xff);
    }

    public static long bytesToLong( byte[] array )
    {
    return ((((long) array[ 0] & 0xff) << 56)
    | (((long) array[ 1] & 0xff) << 48)
    | (((long) array[ 2] & 0xff) << 40)
    | (((long) array[ 3] & 0xff) << 32)
    | (((long) array[ 4] & 0xff) << 24)
    | (((long) array[ 5] & 0xff) << 16)
    | (((long) array[ 6] & 0xff) << 8 )
    | (((long) array[ 7] & 0xff) << 0));
    }

    public static long bytesToLong( byte[] array, int offset )
    {
    return ((((long) array[offset + 0] & 0xff) << 56)
    | (((long) array[offset + 1] & 0xff) << 48)
    | (((long) array[offset + 2] & 0xff) << 40)
    | (((long) array[offset + 3] & 0xff) << 32)
    | (((long) array[offset + 4] & 0xff) << 24)
    | (((long) array[offset + 5] & 0xff) << 16)
    | (((long) array[offset + 6] & 0xff) << 8 )
    | (((long) array[offset + 7] & 0xff) << 0));
    }

    public static byte[] intToBytes(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;
    }

    public static void intToBytes( int n, byte[] array, int offset ){
    array[3+offset] = (byte) (n & 0xff);
    array[2+offset] = (byte) (n >> 8 & 0xff);
    array[1+offset] = (byte) (n >> 16 & 0xff);
    array[offset] = (byte) (n >> 24 & 0xff);
    }

    public static int bytesToInt(byte b[]) {
    return b[3] & 0xff
    | (b[2] & 0xff) << 8
    | (b[1] & 0xff) << 16
    | (b[0] & 0xff) << 24;
    }

    public static int bytesToInt(byte b[], int offset) {
    return b[offset+3] & 0xff
    | (b[offset+2] & 0xff) << 8
    | (b[offset+1] & 0xff) << 16
    | (b[offset] & 0xff) << 24;
    }

    public static byte[] uintToBytes( long 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;
    }

    public static void uintToBytes( long n, byte[] array, int offset ){
    array[3+offset] = (byte) (n );
    array[2+offset] = (byte) (n >> 8 & 0xff);
    array[1+offset] = (byte) (n >> 16 & 0xff);
    array[offset] = (byte) (n >> 24 & 0xff);
    }

    public static long bytesToUint(byte[] array) {
    return ((long) (array[3] & 0xff))
    | ((long) (array[2] & 0xff)) << 8
    | ((long) (array[1] & 0xff)) << 16
    | ((long) (array[0] & 0xff)) << 24;
    }

    public static long bytesToUint(byte[] array, int offset) {
    return ((long) (array[offset+3] & 0xff))
    | ((long) (array[offset+2] & 0xff)) << 8
    | ((long) (array[offset+1] & 0xff)) << 16
    | ((long) (array[offset] & 0xff)) << 24;
    }

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

    public static void shortToBytes(short n, byte[] array, int offset ) {
    array[offset+1] = (byte) ( n & 0xff);
    array[offset] = (byte) ((n >> 8 ) & 0xff);
    }

    public static short bytesToShort(byte[] b){
    return (short)( b[1] & 0xff
    |(b[0] & 0xff) << 8 );
    }

    public static short bytesToShort(byte[] b, int offset){
    return (short)( b[offset+1] & 0xff
    |(b[offset] & 0xff) << 8 );
    }

    public static byte[] ushortToBytes(int n) {
    byte[] b = new byte[2];
    b[1] = (byte) ( n & 0xff);
    b[0] = (byte) ((n >> 8 ) & 0xff);
    return b;
    }

    public static void ushortToBytes(int n, byte[] array, int offset ) {
    array[offset+1] = (byte) ( n & 0xff);
    array[offset] = (byte) ((n >> 8 ) & 0xff);
    }

    public static int bytesToUshort(byte b[]) {
    return b[1] & 0xff
    | (b[0] & 0xff) << 8;
    }

    public static int bytesToUshort(byte b[], int offset) {
    return b[offset+1] & 0xff
    | (b[offset] & 0xff) << 8;
    }

    public static byte[] ubyteToBytes( int n ){
    byte[] b = new byte[1];
    b[0] = (byte) (n & 0xff);
    return b;
    }

    public static void ubyteToBytes( int n, byte[] array, int offset ){
    array[0] = (byte) (n & 0xff);
    }

    public static int bytesToUbyte( byte[] array ){
    return array[0] & 0xff;
    }

    public static int bytesToUbyte( byte[] array, int offset ){
    return array[offset] & 0xff;
    }
    // char 类型、 float、double 类型和 byte[] 数组之间的转换关系还需继续研究实现。
    }


    测试程序如下:

    public class ByteConvertTest {

    public static String byte2Hex(byte[] buf)
    {
    StringBuffer strbuf = new StringBuffer();
    strbuf.append("{");
    for (byte b : buf)
    {
    if (b == 0)
    {
    strbuf.append("00");
    }
    else if (b == -1)
    {
    strbuf.append("FF");
    }
    else
    {
    String str = Integer.toHexString(b).toUpperCase();
    // sb.append(a);
    if (str.length() == 8 )
    {
    str = str.substring(6, 8 );
    }
    else if (str.length() < 2)
    {
    str = "0" + str;
    }
    strbuf.append(str);
    }
    strbuf.append(" ");
    }
    strbuf.append("}");
    return strbuf.toString();
    }

    public static byte[] longToBytes(long n) {
    byte[] b = new byte[8];
    b[7] = (byte) (n & 0xff);
    b[6] = (byte) (n >> 8 & 0xff);
    b[5] = (byte) (n >> 16 & 0xff);
    b[4] = (byte) (n >> 24 & 0xff);
    b[3] = (byte) (n >> 32 & 0xff);
    b[2] = (byte) (n >> 40 & 0xff);
    b[1] = (byte) (n >> 48 & 0xff);
    b[0] = (byte) (n >> 56 & 0xff);
    return b;
    }

    public static long bytesToLong( byte[] array )
    {
    return ((((long) array[ 0] & 0xff) << 56)
    | (((long) array[ 1] & 0xff) << 48)
    | (((long) array[ 2] & 0xff) << 40)
    | (((long) array[ 3] & 0xff) << 32)
    | (((long) array[ 4] & 0xff) << 24)
    | (((long) array[ 5] & 0xff) << 16)
    | (((long) array[ 6] & 0xff) << 8 )
    | (((long) array[ 7] & 0xff) ));
    }

    public static int bytesToInt(byte b[]) {
    return b[3] & 0xff
    | (b[2] & 0xff) << 8
    | (b[1] & 0xff) << 16
    | (b[0] & 0xff) << 24;
    }

    public static long bytesToUint(byte[] array) {
    return ((long) (array[3] & 0xff))
    | ((long) (array[2] & 0xff)) << 8
    | ((long) (array[1] & 0xff)) << 16
    | ((long) (array[0] & 0xff)) << 24;
    }

    public static byte[] uintToBytes( long 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;
    }


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

    public static short bytesToShort(byte[] b){
    return (short)( b[1] & 0xff
    |(b[0] & 0xff) << 8 );
    }

    static void testShortConvert(){
    System.out.println("=================== short convert =============");
    System.out.println("byte2Hex(shortToBytes((short)0x11f2))"+byte2Hex(shortToBytes((short)0x11f2)));
    System.out.print("println 0x11f2:");
    System.out.println((short)0x11f2);
    System.out.println("byte2Hex(shortToBytes((short)0xf1f2))"+byte2Hex(shortToBytes((short)0xf1f2)));
    System.out.print("println 0xf1f2:");
    System.out.println((short)0xf1f2);
    System.out.print("println bytesToShort(shortToBytes((short)0x11f2)):");
    System.out.println((short)bytesToShort(shortToBytes((short)0x11f2)));
    System.out.print("println bytesToShort(shortToBytes((short)0xf1f2)):");
    System.out.println((short)bytesToShort(shortToBytes((short)0xf1f2)));
    }


    public static byte[] ushortToBytes(int n) {
    byte[] b = new byte[2];
    b[1] = (byte) (n & 0xff);
    b[0] = (byte) (n >> 8 & 0xff);
    return b;
    }


    public static int bytesToUshort(byte b[]) {
    return b[1] & 0xff
    | (b[0] & 0xff) << 8;
    }

    static void testUshortConvert(){
    System.out.println("=================== Ushort convert =============");
    System.out.println("byte2Hex(ushortToBytes(0x11f2))"+byte2Hex(ushortToBytes(0x11f2)));
    System.out.print("println 0x11f2:");
    System.out.println(0x11f2);
    System.out.println("byte2Hex(ushortToBytes(0xf1f2))"+byte2Hex(ushortToBytes(0xf1f2)));
    System.out.print("println 0xf1f2:");
    System.out.println(0xf1f2);
    System.out.print("println bytesToUshort(ushortToBytes(0x11f2)):");
    System.out.println(bytesToUshort(ushortToBytes(0x11f2)));
    System.out.print("println bytesToUshort(ushortToBytes(0xf1f2)):");
    System.out.println(bytesToUshort(ushortToBytes(0xf1f2)));
    }

    public static byte[] ubyteToBytes( int n ){
    byte[] b = new byte[1];
    b[0] = (byte) (n & 0xff);
    return b;
    }

    public static int bytesToUbyte( byte[] array ){
    return array[0] & 0xff;
    }

    static void testUbyteConvert(){
    System.out.println("=================== Ubyte convert =============");
    System.out.println("byte2Hex(ubyteToBytes(0x1112))"+byte2Hex(ubyteToBytes(0x1112)));
    System.out.print("println 0x1112:");
    System.out.println(0x1112);
    System.out.println("byte2Hex(ubyteToBytes(0xf2))"+byte2Hex(ubyteToBytes(0xf2)));
    System.out.print("println 0xf2:");
    System.out.println(0xf2);
    System.out.print("println bytesToUbyte(ubyteToBytes(0x1112)):");
    System.out.println(bytesToUbyte(ubyteToBytes(0x1112)));
    System.out.print("println bytesToUbyte(ubyteToBytes(0xf1f2)):");
    System.out.println(bytesToUbyte(ubyteToBytes(0xf1f2)));
    }


    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    byte[] array = new byte[4];
    array[3] = (byte) 0xF4;
    array[2] = 0x13;
    array[1] = 0x12;
    array[0] = 0x11;

    System.out.println("=================== Integer bytes =============");

    System.out.println("the bytes is:"+byte2Hex(array) );
    System.out.print("println bytesToInt :");
    System.out.println( bytesToInt(array));
    System.out.printf("printf bytesToInt :%X\n", bytesToInt(array));

    System.out.println("=================== long bytes =============");
    byte[] longBytes = new byte[8];

    longBytes[7] = (byte) 0xf7;
    longBytes[6] = (byte) 0x16;
    longBytes[5] = (byte) 0xf5;
    longBytes[4] = (byte) 0x14;
    longBytes[3] = (byte) 0xf3;
    longBytes[2] = (byte) 0x12;
    longBytes[1] = (byte) 0xf1;
    longBytes[0] = (byte) 0x10;


    System.out.println( "the bytes is:"+byte2Hex(longBytes) );
    System.out.printf("printf bytesToLong:%X\n",bytesToLong(longBytes));

    System.out.println("=================byte to long ================");

    byte b = (byte)0xf1;
    System.out.print("Println the byte:");
    System.out.println(b);
    System.out.printf("Printf the byte:%X\n",b);
    long l = b;
    System.out.print("Println byte to long:");
    System.out.println(l);
    System.out.printf("printf byte to long:%X\n",l);

    System.out.println("================= uint Bytes ================");

    byte[] uint = new byte[4];
    uint[3] = (byte) 0xf3;
    uint[2] = (byte) 0x12;
    uint[1] = (byte) 0xf1;
    uint[0] = (byte) 0xFF;

    System.out.println( "the bytes is:"+byte2Hex(uint) );
    System.out.printf("printf bytesToUint:%X\n",bytesToUint(uint));
    System.out.print("Println bytesToUint:");
    System.out.println(bytesToUint(uint));
    System.out.println("byte2Hex(uintToBytes(0x11f2f3f4f5f6f7f8l)):"+byte2Hex(uintToBytes(0x11f2f3f4f5f6f7f8l)));

    System.out.println("===============Long Integer==============");
    System.out.print("println 0x11f2f3f4f5f6f7f8l:");
    System.out.println(0x11f2f3f4f5f6f7f8l);
    System.out.printf("Printf 0x11f2f3f4f5f6f7f8l:%X\n",0x11f2f3f4f5f6f7f8l);
    System.out.println("println byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l))"+byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l)));
    // 注意,下面的这行,并不能获得正确的uint。
    System.out.printf("printf bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l):%X\n",bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l)));

    System.out.println("===============bytesToLong(longToBytes())==============");
    System.out.println(bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l)));
    System.out.printf("%X\n",bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l)));

    testShortConvert();
    testUshortConvert();
    testUbyteConvert();
    }

    }
    展开全文
  • java整型数与网络字节序的转换 /** * java整型数与网络字节序的 byte[] 数组转换关系 * 工作项目需要在java和c/c++之间进行socket...如果发送方和接收方都是同种语言,则一般只涉及到字节序调整。而对于java和...

    java整型数与网络字节序的转换

    /**
     * java整型数与网络字节序的 byte[] 数组转换关系
     * 工作项目需要在java和c/c++之间进行socket通信,socket通信是以字节流或者字节包进行的,socket发送方须将数据转换为字节流或者字节包,
     * 而接收方则将字节流和字节包再转换回相应的数据类型。如果发送方和接收方都是同种语言,则一般只涉及到字节序的调整。而对于java和c/c++的通信,
     * 则情况就要复杂一些,主要是因为java中没有unsigned类型,并且java和c在某些数据类型上的长度不一致。
     * 本文就是针对这种情况,整理了java数据类型和网络字节流或字节包(相当于java的byte数组)之间转换方法。实际上网上这方面的资料不少,但往往不全,
     * 甚至有些有错误,于是就花了点时间对java整型数和网络字节序的byte[]之间转换的各种情况做了一些验证和整理。
     * 
     * @author Administrator
     *
     */
    public class ByteConvert {
    	// 以下 是整型数 和 网络字节序的 byte[] 数组之间的转换
    	public static byte[] longToBytes(long n) {
    		byte[] b = new byte[8];
    		b[7] = (byte) (n & 0xff);
    		b[6] = (byte) (n >> 8 & 0xff);
    		b[5] = (byte) (n >> 16 & 0xff);
    		b[4] = (byte) (n >> 24 & 0xff);
    		b[3] = (byte) (n >> 32 & 0xff);
    		b[2] = (byte) (n >> 40 & 0xff);
    		b[1] = (byte) (n >> 48 & 0xff);
    		b[0] = (byte) (n >> 56 & 0xff);
    		return b;
    	}
    
        public static void longToBytes( long n, byte[] array, int offset ){
            array[7+offset] = (byte) (n & 0xff);
            array[6+offset] = (byte) (n >> 8 & 0xff);
            array[5+offset] = (byte) (n >> 16 & 0xff);
            array[4+offset] = (byte) (n >> 24 & 0xff);
            array[3+offset] = (byte) (n >> 32 & 0xff);
            array[2+offset] = (byte) (n >> 40 & 0xff);
            array[1+offset] = (byte) (n >> 48 & 0xff);
            array[0+offset] = (byte) (n >> 56 & 0xff);
        }
        
        public static long bytesToLong( byte[] array ){
            return ((((long) array[ 0] & 0xff) << 56)
                  | (((long) array[ 1] & 0xff) << 48)
                  | (((long) array[ 2] & 0xff) << 40)
                  | (((long) array[ 3] & 0xff) << 32)
                  | (((long) array[ 4] & 0xff) << 24)
                  | (((long) array[ 5] & 0xff) << 16)
                  | (((long) array[ 6] & 0xff) << 8) 
                  | (((long) array[ 7] & 0xff) << 0));        
        }
        
        public static long bytesToLong( byte[] array, int offset ){
            return ((((long) array[offset + 0] & 0xff) << 56)
                  | (((long) array[offset + 1] & 0xff) << 48)
                  | (((long) array[offset + 2] & 0xff) << 40)
                  | (((long) array[offset + 3] & 0xff) << 32)
                  | (((long) array[offset + 4] & 0xff) << 24)
                  | (((long) array[offset + 5] & 0xff) << 16)
                  | (((long) array[offset + 6] & 0xff) << 8) 
                  | (((long) array[offset + 7] & 0xff) << 0));            
        }
        
        public static byte[] intToBytes(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;
        }
        
        public static void intToBytes( int n, byte[] array, int offset ){
            array[3+offset] = (byte) (n & 0xff);
            array[2+offset] = (byte) (n >> 8 & 0xff);
            array[1+offset] = (byte) (n >> 16 & 0xff);
            array[offset] = (byte) (n >> 24 & 0xff);
        }    
    
        public static int bytesToInt(byte b[]) {
            return    b[3] & 0xff 
                   | (b[2] & 0xff) << 8 
                   | (b[1] & 0xff) << 16
                   | (b[0] & 0xff) << 24;
        }
    
        public static int bytesToInt(byte b[], int offset) {
            return    b[offset+3] & 0xff 
                   | (b[offset+2] & 0xff) << 8 
                   | (b[offset+1] & 0xff) << 16
                   | (b[offset] & 0xff) << 24;
        }
    
        public static byte[] uintToBytes( long 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;
        }
    
        public static void uintToBytes( long n, byte[] array, int offset ){
            array[3+offset] = (byte) (n );
            array[2+offset] = (byte) (n >> 8 & 0xff);
            array[1+offset] = (byte) (n >> 16 & 0xff);
            array[offset]   = (byte) (n >> 24 & 0xff);
        }
    
        public static long bytesToUint(byte[] array) {  
            return ((long) (array[3] & 0xff))  
                 | ((long) (array[2] & 0xff)) << 8  
                 | ((long) (array[1] & 0xff)) << 16  
                 | ((long) (array[0] & 0xff)) << 24;  
        }
    
        public static long bytesToUint(byte[] array, int offset) {   
            return ((long) (array[offset+3] & 0xff))  
                  | ((long) (array[offset+2] & 0xff)) << 8  
                 | ((long) (array[offset+1] & 0xff)) << 16  
                 | ((long) (array[offset]   & 0xff)) << 24;  
        }
    
        public static byte[] shortToBytes(short n) {
            byte[] b = new byte[2];
            b[1] = (byte) ( n       & 0xff);
            b[0] = (byte) ((n >> 8) & 0xff);
            return b;
        }
        
        public static void shortToBytes(short n, byte[] array, int offset ) {        
            array[offset+1] = (byte) ( n       & 0xff);
            array[offset] = (byte) ((n >> 8) & 0xff);
        }
        
        public static short bytesToShort(byte[] b){
            return (short)( b[1] & 0xff
                          |(b[0] & 0xff) << 8 ); 
        }    
    
        public static short bytesToShort(byte[] b, int offset){
            return (short)( b[offset+1] & 0xff
                          |(b[offset]    & 0xff) << 8 ); 
        }
    
        public static byte[] ushortToBytes(int n) {
            byte[] b = new byte[2];
            b[1] = (byte) ( n       & 0xff);
            b[0] = (byte) ((n >> 8) & 0xff);
            return b;
        }    
    
        public static void ushortToBytes(int n, byte[] array, int offset ) {
            array[offset+1] = (byte) ( n       & 0xff);
            array[offset] = (byte)   ((n >> 8) & 0xff);
        }
    
        public static int bytesToUshort(byte b[]) {
            return    b[1] & 0xff 
                   | (b[0] & 0xff) << 8;
        }    
    
        public static int bytesToUshort(byte b[], int offset) {
            return    b[offset+1] & 0xff 
                   | (b[offset]   & 0xff) << 8;
        }    
    
        public static byte[] ubyteToBytes( int n ){
            byte[] b = new byte[1];
            b[0] = (byte) (n & 0xff);
            return b;
        }
    
        public static void ubyteToBytes( int n, byte[] array, int offset ){
            array[0] = (byte) (n & 0xff);
        }
    
        public static int bytesToUbyte( byte[] array ){            
            return array[0] & 0xff;
        }        
    
        public static int bytesToUbyte( byte[] array, int offset ){            
            return array[offset] & 0xff;
        }    
        // char 类型、 float、double 类型和 byte[] 数组之间的转换关系还需继续研究实现。 
    }
    
    展开全文
  • java整型数与网络字节序的 byte[] 数组转换关系 工作项目需要在java和c/c++...如果发送方和接收方都是同种语言,则一般只涉及到字节序调整。而对于java和c/c++的通信,则情况就要复杂一些,主要是因为java中没...

    java整型数与网络字节序的 byte[] 数组转换关系

      工作项目需要在java和c/c++之间进行socket通信,socket通信是以字节流或者字节包进行的,socket发送方须将数据转换为字节流或者字节包,而接收方则将字节流和字节包再转换回相应的数据类型。如果发送方和接收方都是同种语言,则一般只涉及到字节序的调整。而对于java和c/c++的通信,则情况就要复杂一些,主要是因为java中没有unsigned类型,并且java和c在某些数据类型上的长度不一致。

      本文就是针对这种情况,整理了java数据类型和网络字节流或字节包(相当于java的byte数组)之间转换方法。实际上网上这方面的资料不少,但往往不全,甚至有些有错误,于是就花了点时间对java整型数和网络字节序的byte[]之间转换的各种情况做了一些验证和整理。整理出来的函数如下:

     

     

    public class ByteConvert {
        // 以下 是整型数 和 网络字节序的  byte[] 数组之间的转换
        public static byte[] longToBytes(long n) {
            byte[] b = new byte[8];
            b[7] = (byte) (n & 0xff);
            b[6] = (byte) (n >> 8  & 0xff);
            b[5] = (byte) (n >> 16 & 0xff);
            b[4] = (byte) (n >> 24 & 0xff);
            b[3] = (byte) (n >> 32 & 0xff);
            b[2] = (byte) (n >> 40 & 0xff);
            b[1] = (byte) (n >> 48 & 0xff);
            b[0] = (byte) (n >> 56 & 0xff);
            return b;
        }
        
        public static void longToBytes( long n, byte[] array, int offset ){
            array[7+offset] = (byte) (n & 0xff);
            array[6+offset] = (byte) (n >> 8 & 0xff);
            array[5+offset] = (byte) (n >> 16 & 0xff);
            array[4+offset] = (byte) (n >> 24 & 0xff);
            array[3+offset] = (byte) (n >> 32 & 0xff);
            array[2+offset] = (byte) (n >> 40 & 0xff);
            array[1+offset] = (byte) (n >> 48 & 0xff);
            array[0+offset] = (byte) (n >> 56 & 0xff);
        }
        
        public static long bytesToLong( byte[] array )
        {
            return ((((long) array[ 0] & 0xff) << 56)
                  | (((long) array[ 1] & 0xff) << 48)
                  | (((long) array[ 2] & 0xff) << 40)
                  | (((long) array[ 3] & 0xff) << 32)
                  | (((long) array[ 4] & 0xff) << 24)
                  | (((long) array[ 5] & 0xff) << 16)
                  | (((long) array[ 6] & 0xff) << 8) 
                  | (((long) array[ 7] & 0xff) << 0));        
        }
        
        public static long bytesToLong( byte[] array, int offset )
        {
            return ((((long) array[offset + 0] & 0xff) << 56)
                  | (((long) array[offset + 1] & 0xff) << 48)
                  | (((long) array[offset + 2] & 0xff) << 40)
                  | (((long) array[offset + 3] & 0xff) << 32)
                  | (((long) array[offset + 4] & 0xff) << 24)
                  | (((long) array[offset + 5] & 0xff) << 16)
                  | (((long) array[offset + 6] & 0xff) << 8) 
                  | (((long) array[offset + 7] & 0xff) << 0));            
        }
        
        public static byte[] intToBytes(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;
        }
        
        public static void intToBytes( int n, byte[] array, int offset ){
            array[3+offset] = (byte) (n & 0xff);
            array[2+offset] = (byte) (n >> 8 & 0xff);
            array[1+offset] = (byte) (n >> 16 & 0xff);
            array[offset] = (byte) (n >> 24 & 0xff);
        }    
        public static int bytesToInt(byte b[]) {
            return    b[3] & 0xff 
                   | (b[2] & 0xff) << 8 
                   | (b[1] & 0xff) << 16
                   | (b[0] & 0xff) << 24;
        }
        public static int bytesToInt(byte b[], int offset) {
            return    b[offset+3] & 0xff 
                   | (b[offset+2] & 0xff) << 8 
                   | (b[offset+1] & 0xff) << 16
                   | (b[offset] & 0xff) << 24;
        }
        public static byte[] uintToBytes( long 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;
        }
        public static void uintToBytes( long n, byte[] array, int offset ){
            array[3+offset] = (byte) (n );
            array[2+offset] = (byte) (n >> 8 & 0xff);
            array[1+offset] = (byte) (n >> 16 & 0xff);
            array[offset]   = (byte) (n >> 24 & 0xff);
        }
        public static long bytesToUint(byte[] array) {  
            return ((long) (array[3] & 0xff))  
                 | ((long) (array[2] & 0xff)) << 8  
                 | ((long) (array[1] & 0xff)) << 16  
                 | ((long) (array[0] & 0xff)) << 24;  
        }
        public static long bytesToUint(byte[] array, int offset) {   
            return ((long) (array[offset+3] & 0xff))  
                  | ((long) (array[offset+2] & 0xff)) << 8  
                 | ((long) (array[offset+1] & 0xff)) << 16  
                 | ((long) (array[offset]   & 0xff)) << 24;  
        }
        public static byte[] shortToBytes(short n) {
            byte[] b = new byte[2];
            b[1] = (byte) ( n       & 0xff);
            b[0] = (byte) ((n >> 8) & 0xff);
            return b;
        }
        
        public static void shortToBytes(short n, byte[] array, int offset ) {        
            array[offset+1] = (byte) ( n       & 0xff);
            array[offset] = (byte) ((n >> 8) & 0xff);
        }
        
        public static short bytesToShort(byte[] b){
            return (short)( b[1] & 0xff
                          |(b[0] & 0xff) << 8 ); 
        }    
        public static short bytesToShort(byte[] b, int offset){
            return (short)( b[offset+1] & 0xff
                          |(b[offset]    & 0xff) << 8 ); 
        }
        public static byte[] ushortToBytes(int n) {
            byte[] b = new byte[2];
            b[1] = (byte) ( n       & 0xff);
            b[0] = (byte) ((n >> 8) & 0xff);
            return b;
        }    
        public static void ushortToBytes(int n, byte[] array, int offset ) {
            array[offset+1] = (byte) ( n       & 0xff);
            array[offset] = (byte)   ((n >> 8) & 0xff);
        }
        public static int bytesToUshort(byte b[]) {
            return    b[1] & 0xff 
                   | (b[0] & 0xff) << 8;
        }    
        public static int bytesToUshort(byte b[], int offset) {
            return    b[offset+1] & 0xff 
                   | (b[offset]   & 0xff) << 8;
        }    
        public static byte[] ubyteToBytes( int n ){
            byte[] b = new byte[1];
            b[0] = (byte) (n & 0xff);
            return b;
        }
        public static void ubyteToBytes( int n, byte[] array, int offset ){
            array[0] = (byte) (n & 0xff);
        }
        public static int bytesToUbyte( byte[] array ){            
            return array[0] & 0xff;
        }        
        public static int bytesToUbyte( byte[] array, int offset ){            
            return array[offset] & 0xff;
        }    
        // char 类型、 float、double 类型和 byte[] 数组之间的转换关系还需继续研究实现。 
    }
    测试程序如下:
    public class ByteConvertTest {
        
        public static String byte2Hex(byte[] buf) 
        {
            StringBuffer strbuf = new StringBuffer();
            strbuf.append("{");
            for (byte b : buf) 
            {
                if (b == 0) 
                {
                    strbuf.append("00");
                } 
                else if (b == -1) 
                {
                    strbuf.append("FF");
                } 
                else 
                {
                    String str = Integer.toHexString(b).toUpperCase();
                    // sb.append(a);
                    if (str.length() == 8) 
                    {
                        str = str.substring(6, 8);
                    } 
                    else if (str.length() < 2) 
                    {
                        str = "0" + str;
                    }
                    strbuf.append(str);
                }
                strbuf.append(" ");
            }
            strbuf.append("}");
            return strbuf.toString();
        }    
        public static byte[] longToBytes(long n) {
            byte[] b = new byte[8];
            b[7] = (byte) (n & 0xff);
            b[6] = (byte) (n >> 8  & 0xff);
            b[5] = (byte) (n >> 16 & 0xff);
            b[4] = (byte) (n >> 24 & 0xff);
            b[3] = (byte) (n >> 32 & 0xff);
            b[2] = (byte) (n >> 40 & 0xff);
            b[1] = (byte) (n >> 48 & 0xff);
            b[0] = (byte) (n >> 56 & 0xff);
            return b;
        }
        public static long bytesToLong( byte[] array )
        {
            return ((((long) array[ 0] & 0xff) << 56)
                  | (((long) array[ 1] & 0xff) << 48)
                  | (((long) array[ 2] & 0xff) << 40)
                  | (((long) array[ 3] & 0xff) << 32)
                  | (((long) array[ 4] & 0xff) << 24)
                  | (((long) array[ 5] & 0xff) << 16)
                  | (((long) array[ 6] & 0xff) << 8) 
                  | (((long) array[ 7] & 0xff) ));        
        }
        
        public static int bytesToInt(byte b[]) {
            return    b[3] & 0xff 
                   | (b[2] & 0xff) << 8 
                   | (b[1] & 0xff) << 16
                   | (b[0] & 0xff) << 24;
        }
        public static long bytesToUint(byte[] array) {  
            return ((long) (array[3] & 0xff))  
                 | ((long) (array[2] & 0xff)) << 8  
                 | ((long) (array[1] & 0xff)) << 16  
                 | ((long) (array[0] & 0xff)) << 24;  
        }
        public static byte[] uintToBytes( long 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;
        }
        
        public static byte[] shortToBytes(short n) {
            byte[] b = new byte[2];
            b[1] = (byte) ( n       & 0xff);
            b[0] = (byte) ((n >> 8) & 0xff);
            return b;
        }
        
        public static short bytesToShort(byte[] b){
            return (short)( b[1] & 0xff
                          |(b[0] & 0xff) << 8 ); 
        }
        
        static void testShortConvert(){
            System.out.println("=================== short convert =============");
            System.out.println("byte2Hex(shortToBytes((short)0x11f2))"+byte2Hex(shortToBytes((short)0x11f2)));        
            System.out.print("println 0x11f2:");
            System.out.println((short)0x11f2);        
            System.out.println("byte2Hex(shortToBytes((short)0xf1f2))"+byte2Hex(shortToBytes((short)0xf1f2)));        
            System.out.print("println 0xf1f2:");
            System.out.println((short)0xf1f2);            
            System.out.print("println bytesToShort(shortToBytes((short)0x11f2)):");
            System.out.println((short)bytesToShort(shortToBytes((short)0x11f2)));            
            System.out.print("println bytesToShort(shortToBytes((short)0xf1f2)):");
            System.out.println((short)bytesToShort(shortToBytes((short)0xf1f2)));        
        }
        
        public static byte[] ushortToBytes(int n) {
            byte[] b = new byte[2];
            b[1] = (byte) (n & 0xff);
            b[0] = (byte) (n >> 8 & 0xff);
            return b;
        }
        
        public static int bytesToUshort(byte b[]) {
            return    b[1] & 0xff 
                   | (b[0] & 0xff) << 8;
        }
        static void testUshortConvert(){
            System.out.println("=================== Ushort convert =============");
            System.out.println("byte2Hex(ushortToBytes(0x11f2))"+byte2Hex(ushortToBytes(0x11f2)));        
            System.out.print("println 0x11f2:");
            System.out.println(0x11f2);        
            System.out.println("byte2Hex(ushortToBytes(0xf1f2))"+byte2Hex(ushortToBytes(0xf1f2)));        
            System.out.print("println 0xf1f2:");
            System.out.println(0xf1f2);            
            System.out.print("println bytesToUshort(ushortToBytes(0x11f2)):");
            System.out.println(bytesToUshort(ushortToBytes(0x11f2)));            
            System.out.print("println bytesToUshort(ushortToBytes(0xf1f2)):");
            System.out.println(bytesToUshort(ushortToBytes(0xf1f2)));        
        }
        
        public static byte[] ubyteToBytes( int n ){
            byte[] b = new byte[1];
            b[0] = (byte) (n & 0xff);
            return b;
        }
        public static int bytesToUbyte( byte[] array ){            
            return array[0] & 0xff;
        }    
        static void testUbyteConvert(){
            System.out.println("=================== Ubyte convert =============");
            System.out.println("byte2Hex(ubyteToBytes(0x1112))"+byte2Hex(ubyteToBytes(0x1112)));        
            System.out.print("println 0x1112:");
            System.out.println(0x1112);        
            System.out.println("byte2Hex(ubyteToBytes(0xf2))"+byte2Hex(ubyteToBytes(0xf2)));        
            System.out.print("println 0xf2:");
            System.out.println(0xf2);            
            System.out.print("println bytesToUbyte(ubyteToBytes(0x1112)):");
            System.out.println(bytesToUbyte(ubyteToBytes(0x1112)));            
            System.out.print("println bytesToUbyte(ubyteToBytes(0xf1f2)):");
            System.out.println(bytesToUbyte(ubyteToBytes(0xf1f2)));        
        }
        
        
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub        
            byte[] array = new byte[4];
            array[3] = (byte) 0xF4;
            array[2] = 0x13;
            array[1] = 0x12;
            array[0] = 0x11;
            
            System.out.println("=================== Integer bytes =============");
            
            System.out.println("the bytes is:"+byte2Hex(array) );
            System.out.print("println bytesToInt :");
            System.out.println( bytesToInt(array));
            System.out.printf("printf bytesToInt :%X\n", bytesToInt(array));
            
            System.out.println("=================== long bytes =============");
            byte[] longBytes = new byte[8];
            
            longBytes[7] = (byte) 0xf7;
            longBytes[6] = (byte) 0x16;
            longBytes[5] = (byte) 0xf5;
            longBytes[4] = (byte) 0x14;
            longBytes[3] = (byte) 0xf3;
            longBytes[2] = (byte) 0x12;
            longBytes[1] = (byte) 0xf1;
            longBytes[0] = (byte) 0x10;
            
            System.out.println( "the bytes is:"+byte2Hex(longBytes) );
            System.out.printf("printf bytesToLong:%X\n",bytesToLong(longBytes));
            
            System.out.println("=================byte to long ================");
            
            byte b = (byte)0xf1;
            System.out.print("Println the byte:");
            System.out.println(b);
            System.out.printf("Printf the byte:%X\n",b);
            long l = b;
            System.out.print("Println byte to long:");
            System.out.println(l);
            System.out.printf("printf byte to long:%X\n",l);
            
            System.out.println("================= uint Bytes ================");
            
            byte[] uint = new byte[4];
            uint[3] = (byte) 0xf3;
            uint[2] = (byte) 0x12;
            uint[1] = (byte) 0xf1;
            uint[0] = (byte) 0xFF;
            
            System.out.println( "the bytes is:"+byte2Hex(uint) );
            System.out.printf("printf bytesToUint:%X\n",bytesToUint(uint));
            System.out.print("Println bytesToUint:");
            System.out.println(bytesToUint(uint));
            System.out.println("byte2Hex(uintToBytes(0x11f2f3f4f5f6f7f8l)):"+byte2Hex(uintToBytes(0x11f2f3f4f5f6f7f8l)));
            
            System.out.println("===============Long Integer==============");        
            System.out.print("println 0x11f2f3f4f5f6f7f8l:");
            System.out.println(0x11f2f3f4f5f6f7f8l);        
            System.out.printf("Printf 0x11f2f3f4f5f6f7f8l:%X\n",0x11f2f3f4f5f6f7f8l);
            System.out.println("println byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l))"+byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l)));
            // 注意,下面的这行,并不能获得正确的uint。
            System.out.printf("printf bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l):%X\n",bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l)));
            
            System.out.println("===============bytesToLong(longToBytes())==============");
            System.out.println(bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l)));
            System.out.printf("%X\n",bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l)));
            
            testShortConvert();
            testUshortConvert();
            testUbyteConvert();
        }
    }

     

    转载于:https://www.cnblogs.com/wsfjlagr/p/4582171.html

    展开全文
  • 工作项目需要在java和c/c++之间...如果发送方和接收方都是同种语言,则一般只涉及到字节序调整。而对于java和c/c++的通信,则情况就要复杂一些,主要是因为java中没有unsigned类型,并且java和c在某些数据类型上...

      工作项目需要在java和c/c++之间进行socket通信,socket通信是以字节流或者字节包进行的,socket发送方须将数据转换为字节流或者字节包,而接收方则将字节流和字节包再转换回相应的数据类型。如果发送方和接收方都是同种语言,则一般只涉及到字节序的调整。而对于java和c/c++的通信,则情况就要复杂一些,主要是因为java中没有unsigned类型,并且java和c在某些数据类型上的长度不一致。
      本文就是针对这种情况,整理了java数据类型和网络字节流或字节包(相当于java的byte数组)之间转换方法。实际上网上这方面的资料不少,但往往不全,甚至有些有错误,于是就花了点时间对java整型数和网络字节序的byte[]之间转换的各种情况做了一些验证和整理。整理出来的函数如下:

    public class ByteConvert {
        // 以下 是整型数 和 网络字节序的  byte[] 数组之间的转换
        public static byte[] longToBytes(long n) {
            byte[] b = new byte[8];
            b[7] = (byte) (n & 0xff);
            b[6] = (byte) (n >> 8  & 0xff);
            b[5] = (byte) (n >> 16 & 0xff);
            b[4] = (byte) (n >> 24 & 0xff);
            b[3] = (byte) (n >> 32 & 0xff);
            b[2] = (byte) (n >> 40 & 0xff);
            b[1] = (byte) (n >> 48 & 0xff);
            b[0] = (byte) (n >> 56 & 0xff);
            return b;
        }
        
        public static void longToBytes( long n, byte[] array, int offset ){
            array[7+offset] = (byte) (n & 0xff);
            array[6+offset] = (byte) (n >> 8 & 0xff);
            array[5+offset] = (byte) (n >> 16 & 0xff);
            array[4+offset] = (byte) (n >> 24 & 0xff);
            array[3+offset] = (byte) (n >> 32 & 0xff);
            array[2+offset] = (byte) (n >> 40 & 0xff);
            array[1+offset] = (byte) (n >> 48 & 0xff);
            array[0+offset] = (byte) (n >> 56 & 0xff);
        }
        
        public static long bytesToLong( byte[] array )
        {
            return ((((long) array[ 0] & 0xff) << 56)
                  | (((long) array[ 1] & 0xff) << 48)
                  | (((long) array[ 2] & 0xff) << 40)
                  | (((long) array[ 3] & 0xff) << 32)
                  | (((long) array[ 4] & 0xff) << 24)
                  | (((long) array[ 5] & 0xff) << 16)
                  | (((long) array[ 6] & 0xff) << 8) 
                  | (((long) array[ 7] & 0xff) << 0));        
        }
        
        public static long bytesToLong( byte[] array, int offset )
        {
            return ((((long) array[offset + 0] & 0xff) << 56)
                  | (((long) array[offset + 1] & 0xff) << 48)
                  | (((long) array[offset + 2] & 0xff) << 40)
                  | (((long) array[offset + 3] & 0xff) << 32)
                  | (((long) array[offset + 4] & 0xff) << 24)
                  | (((long) array[offset + 5] & 0xff) << 16)
                  | (((long) array[offset + 6] & 0xff) << 8) 
                  | (((long) array[offset + 7] & 0xff) << 0));            
        }
        
        public static byte[] intToBytes(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;
        }
        
        public static void intToBytes( int n, byte[] array, int offset ){
            array[3+offset] = (byte) (n & 0xff);
            array[2+offset] = (byte) (n >> 8 & 0xff);
            array[1+offset] = (byte) (n >> 16 & 0xff);
            array[offset] = (byte) (n >> 24 & 0xff);
        }    
    
        public static int bytesToInt(byte b[]) {
            return    b[3] & 0xff 
                   | (b[2] & 0xff) << 8 
                   | (b[1] & 0xff) << 16
                   | (b[0] & 0xff) << 24;
        }
    
        public static int bytesToInt(byte b[], int offset) {
            return    b[offset+3] & 0xff 
                   | (b[offset+2] & 0xff) << 8 
                   | (b[offset+1] & 0xff) << 16
                   | (b[offset] & 0xff) << 24;
        }
    
        public static byte[] uintToBytes( long 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;
        }
    
        public static void uintToBytes( long n, byte[] array, int offset ){
            array[3+offset] = (byte) (n );
            array[2+offset] = (byte) (n >> 8 & 0xff);
            array[1+offset] = (byte) (n >> 16 & 0xff);
            array[offset]   = (byte) (n >> 24 & 0xff);
        }
    
        public static long bytesToUint(byte[] array) {  
            return ((long) (array[3] & 0xff))  
                 | ((long) (array[2] & 0xff)) << 8  
                 | ((long) (array[1] & 0xff)) << 16  
                 | ((long) (array[0] & 0xff)) << 24;  
        }
    
        public static long bytesToUint(byte[] array, int offset) {   
            return ((long) (array[offset+3] & 0xff))  
                  | ((long) (array[offset+2] & 0xff)) << 8  
                 | ((long) (array[offset+1] & 0xff)) << 16  
                 | ((long) (array[offset]   & 0xff)) << 24;  
        }
    
        public static byte[] shortToBytes(short n) {
            byte[] b = new byte[2];
            b[1] = (byte) ( n       & 0xff);
            b[0] = (byte) ((n >> 8) & 0xff);
            return b;
        }
        
        public static void shortToBytes(short n, byte[] array, int offset ) {        
            array[offset+1] = (byte) ( n       & 0xff);
            array[offset] = (byte) ((n >> 8) & 0xff);
        }
        
        public static short bytesToShort(byte[] b){
            return (short)( b[1] & 0xff
                          |(b[0] & 0xff) << 8 ); 
        }    
    
        public static short bytesToShort(byte[] b, int offset){
            return (short)( b[offset+1] & 0xff
                          |(b[offset]    & 0xff) << 8 ); 
        }
    
        public static byte[] ushortToBytes(int n) {
            byte[] b = new byte[2];
            b[1] = (byte) ( n       & 0xff);
            b[0] = (byte) ((n >> 8) & 0xff);
            return b;
        }    
    
        public static void ushortToBytes(int n, byte[] array, int offset ) {
            array[offset+1] = (byte) ( n       & 0xff);
            array[offset] = (byte)   ((n >> 8) & 0xff);
        }
    
        public static int bytesToUshort(byte b[]) {
            return    b[1] & 0xff 
                   | (b[0] & 0xff) << 8;
        }    
    
        public static int bytesToUshort(byte b[], int offset) {
            return    b[offset+1] & 0xff 
                   | (b[offset]   & 0xff) << 8;
        }    
    
        public static byte[] ubyteToBytes( int n ){
            byte[] b = new byte[1];
            b[0] = (byte) (n & 0xff);
            return b;
        }
    
        public static void ubyteToBytes( int n, byte[] array, int offset ){
            array[0] = (byte) (n & 0xff);
        }
    
        public static int bytesToUbyte( byte[] array ){            
            return array[0] & 0xff;
        }        
    
        public static int bytesToUbyte( byte[] array, int offset ){            
            return array[offset] & 0xff;
        }    
        // char 类型、 float、double 类型和 byte[] 数组之间的转换关系还需继续研究实现。 
    }
    
    
    测试程序如下:
    
    public class ByteConvertTest {
        
        public static String byte2Hex(byte[] buf) 
        {
            StringBuffer strbuf = new StringBuffer();
            strbuf.append("{");
            for (byte b : buf) 
            {
                if (b == 0) 
                {
                    strbuf.append("00");
                } 
                else if (b == -1) 
                {
                    strbuf.append("FF");
                } 
                else 
                {
                    String str = Integer.toHexString(b).toUpperCase();
                    // sb.append(a);
                    if (str.length() == 8) 
                    {
                        str = str.substring(6, 8);
                    } 
                    else if (str.length() < 2) 
                    {
                        str = "0" + str;
                    }
                    strbuf.append(str);
                }
                strbuf.append(" ");
            }
            strbuf.append("}");
            return strbuf.toString();
        }    
    
        public static byte[] longToBytes(long n) {
            byte[] b = new byte[8];
            b[7] = (byte) (n & 0xff);
            b[6] = (byte) (n >> 8  & 0xff);
            b[5] = (byte) (n >> 16 & 0xff);
            b[4] = (byte) (n >> 24 & 0xff);
            b[3] = (byte) (n >> 32 & 0xff);
            b[2] = (byte) (n >> 40 & 0xff);
            b[1] = (byte) (n >> 48 & 0xff);
            b[0] = (byte) (n >> 56 & 0xff);
            return b;
        }
    
        public static long bytesToLong( byte[] array )
        {
            return ((((long) array[ 0] & 0xff) << 56)
                  | (((long) array[ 1] & 0xff) << 48)
                  | (((long) array[ 2] & 0xff) << 40)
                  | (((long) array[ 3] & 0xff) << 32)
                  | (((long) array[ 4] & 0xff) << 24)
                  | (((long) array[ 5] & 0xff) << 16)
                  | (((long) array[ 6] & 0xff) << 8) 
                  | (((long) array[ 7] & 0xff) ));        
        }
        
        public static int bytesToInt(byte b[]) {
            return    b[3] & 0xff 
                   | (b[2] & 0xff) << 8 
                   | (b[1] & 0xff) << 16
                   | (b[0] & 0xff) << 24;
        }
    
        public static long bytesToUint(byte[] array) {  
            return ((long) (array[3] & 0xff))  
                 | ((long) (array[2] & 0xff)) << 8  
                 | ((long) (array[1] & 0xff)) << 16  
                 | ((long) (array[0] & 0xff)) << 24;  
        }
    
        public static byte[] uintToBytes( long 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;
        }
        
    
        public static byte[] shortToBytes(short n) {
            byte[] b = new byte[2];
            b[1] = (byte) ( n       & 0xff);
            b[0] = (byte) ((n >> 8) & 0xff);
            return b;
        }
        
        public static short bytesToShort(byte[] b){
            return (short)( b[1] & 0xff
                          |(b[0] & 0xff) << 8 ); 
        }
        
        static void testShortConvert(){
            System.out.println("=================== short convert =============");
            System.out.println("byte2Hex(shortToBytes((short)0x11f2))"+byte2Hex(shortToBytes((short)0x11f2)));        
            System.out.print("println 0x11f2:");
            System.out.println((short)0x11f2);        
            System.out.println("byte2Hex(shortToBytes((short)0xf1f2))"+byte2Hex(shortToBytes((short)0xf1f2)));        
            System.out.print("println 0xf1f2:");
            System.out.println((short)0xf1f2);            
            System.out.print("println bytesToShort(shortToBytes((short)0x11f2)):");
            System.out.println((short)bytesToShort(shortToBytes((short)0x11f2)));            
            System.out.print("println bytesToShort(shortToBytes((short)0xf1f2)):");
            System.out.println((short)bytesToShort(shortToBytes((short)0xf1f2)));        
        }
        
    
        public static byte[] ushortToBytes(int n) {
            byte[] b = new byte[2];
            b[1] = (byte) (n & 0xff);
            b[0] = (byte) (n >> 8 & 0xff);
            return b;
        }
        
    
        public static int bytesToUshort(byte b[]) {
            return    b[1] & 0xff 
                   | (b[0] & 0xff) << 8;
        }
    
        static void testUshortConvert(){
            System.out.println("=================== Ushort convert =============");
            System.out.println("byte2Hex(ushortToBytes(0x11f2))"+byte2Hex(ushortToBytes(0x11f2)));        
            System.out.print("println 0x11f2:");
            System.out.println(0x11f2);        
            System.out.println("byte2Hex(ushortToBytes(0xf1f2))"+byte2Hex(ushortToBytes(0xf1f2)));        
            System.out.print("println 0xf1f2:");
            System.out.println(0xf1f2);            
            System.out.print("println bytesToUshort(ushortToBytes(0x11f2)):");
            System.out.println(bytesToUshort(ushortToBytes(0x11f2)));            
            System.out.print("println bytesToUshort(ushortToBytes(0xf1f2)):");
            System.out.println(bytesToUshort(ushortToBytes(0xf1f2)));        
        }
        
        public static byte[] ubyteToBytes( int n ){
            byte[] b = new byte[1];
            b[0] = (byte) (n & 0xff);
            return b;
        }
    
        public static int bytesToUbyte( byte[] array ){            
            return array[0] & 0xff;
        }    
    
        static void testUbyteConvert(){
            System.out.println("=================== Ubyte convert =============");
            System.out.println("byte2Hex(ubyteToBytes(0x1112))"+byte2Hex(ubyteToBytes(0x1112)));        
            System.out.print("println 0x1112:");
            System.out.println(0x1112);        
            System.out.println("byte2Hex(ubyteToBytes(0xf2))"+byte2Hex(ubyteToBytes(0xf2)));        
            System.out.print("println 0xf2:");
            System.out.println(0xf2);            
            System.out.print("println bytesToUbyte(ubyteToBytes(0x1112)):");
            System.out.println(bytesToUbyte(ubyteToBytes(0x1112)));            
            System.out.print("println bytesToUbyte(ubyteToBytes(0xf1f2)):");
            System.out.println(bytesToUbyte(ubyteToBytes(0xf1f2)));        
        }
        
        
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub        
            byte[] array = new byte[4];
            array[3] = (byte) 0xF4;
            array[2] = 0x13;
            array[1] = 0x12;
            array[0] = 0x11;
            
            System.out.println("=================== Integer bytes =============");
            
            System.out.println("the bytes is:"+byte2Hex(array) );
            System.out.print("println bytesToInt :");
            System.out.println( bytesToInt(array));
            System.out.printf("printf bytesToInt :%X\n", bytesToInt(array));
            
            System.out.println("=================== long bytes =============");
            byte[] longBytes = new byte[8];
            
            longBytes[7] = (byte) 0xf7;
            longBytes[6] = (byte) 0x16;
            longBytes[5] = (byte) 0xf5;
            longBytes[4] = (byte) 0x14;
            longBytes[3] = (byte) 0xf3;
            longBytes[2] = (byte) 0x12;
            longBytes[1] = (byte) 0xf1;
            longBytes[0] = (byte) 0x10;
            
    
            System.out.println( "the bytes is:"+byte2Hex(longBytes) );
            System.out.printf("printf bytesToLong:%X\n",bytesToLong(longBytes));
            
            System.out.println("=================byte to long ================");
            
            byte b = (byte)0xf1;
            System.out.print("Println the byte:");
            System.out.println(b);
            System.out.printf("Printf the byte:%X\n",b);
            long l = b;
            System.out.print("Println byte to long:");
            System.out.println(l);
            System.out.printf("printf byte to long:%X\n",l);
            
            System.out.println("================= uint Bytes ================");
            
            byte[] uint = new byte[4];
            uint[3] = (byte) 0xf3;
            uint[2] = (byte) 0x12;
            uint[1] = (byte) 0xf1;
            uint[0] = (byte) 0xFF;
            
            System.out.println( "the bytes is:"+byte2Hex(uint) );
            System.out.printf("printf bytesToUint:%X\n",bytesToUint(uint));
            System.out.print("Println bytesToUint:");
            System.out.println(bytesToUint(uint));
            System.out.println("byte2Hex(uintToBytes(0x11f2f3f4f5f6f7f8l)):"+byte2Hex(uintToBytes(0x11f2f3f4f5f6f7f8l)));
            
            System.out.println("===============Long Integer==============");        
            System.out.print("println 0x11f2f3f4f5f6f7f8l:");
            System.out.println(0x11f2f3f4f5f6f7f8l);        
            System.out.printf("Printf 0x11f2f3f4f5f6f7f8l:%X\n",0x11f2f3f4f5f6f7f8l);
            System.out.println("println byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l))"+byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l)));
            // 注意,下面的这行,并不能获得正确的uint。
            System.out.printf("printf bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l):%X\n",bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l)));
            
            System.out.println("===============bytesToLong(longToBytes())==============");
            System.out.println(bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l)));
            System.out.printf("%X\n",bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l)));
            
            testShortConvert();
            testUshortConvert();
            testUbyteConvert();
        }
    
    }

     

     欢迎关注公众号"Devin说",会不定期更新技术知识

     

    转载于:https://www.cnblogs.com/devinzhang/archive/2012/09/28/2707605.html

    展开全文
  • 摘要:本发明公开了一种基于Linux系统读写光模块寄存器的方法,包括:发消息到内核,内核将...否则,判断是否读写寄存器值,若否,异常返回并结束,否则切换I2C访问的寄存器,循环读写并调整字节序,发消息给I2C总线读写寄...
  • 如果发送方和接收方都是同种语言,则一般只涉及到字节序调整。而对于java和c/c++的通信,则情况就要复杂一些,主要是因为java中没有unsigned类型,并且java和c在某些数据类型上的长度不一致...
  • new(n)表示分配n个字节的内存空间。 100 11.7写一函数free,将上题new函数占用的空间释放free(p)表示将p(地址)指向的单元以后的内存段释放。 101 11.8已有a,b两个链表,每个链表中的结点包括学号,成绩。要求把两个...
  • 8.6 字节序 8.7 可移植性和升级 8.8 国际化 8.9 小结 第9章 记法 9.1 数据格式 9.2 正则表达式 9.3 可编程工具 9.4 解释器、编译器和虚拟机 9.5 写程序的程序 9.6 用宏生成代码 9.7 运行中编译 后记 附录:规则汇编 ...
  • 8.6 字节序 161 8.7 可移植性和升级 164 8.8 国际化 165 8.9 小结 167 第9章 记法 169 9.1 数据格式 169 9.2 正则表达式 174 9.3 可编程工具 180 9.4 解释器、编译器和虚拟机 182 9.5 写程序的程序 186 9.6 用宏生成...
  • 程序设计实践 (pdf)

    热门讨论 2011-05-10 16:20:54
    8.6 字节序 161 8.7 可移植性和升级 164 8.8 国际化 165 8.9 小结 167 第9章 记法 169 9.1 数据格式 169 9.2 正则表达式 174 9.3 可编程工具 180 9.4 解释器、编译器和虚拟机 182 9.5 写程序的程序 186 9.6 用宏生成...
  • 程序设计实践

    2011-12-21 14:31:53
    8.6 字节序 161 8.7 可移植性和升级 164 8.8 国际化 165 8.9 小结 167 第9章 记法 169 9.1 数据格式 169 9.2 正则表达式 174 9.3 可编程工具 180 9.4 解释器、编译器和虚拟机 182 9.5 写程序的程序 186 9.6 用宏生成...
  • 8.6 字节序 161 8.7 可移植性和升级 164 8.8 国际化 165 8.9 小结 167 第9章 记法 169 9.1 数据格式 169 9.2 正则表达式 174 9.3 可编程工具 180 9.4 解释器、编译器和虚拟机 182 9.5 写程序的程序 186 ...
  • 8.6 字节序 161 8.7 可移植性和升级 164 8.8 国际化 165 8.9 小结 167 第9章 记法 169 9.1 数据格式 169 9.2 正则表达式 174 9.3 可编程工具 180 9.4 解释器、编译器和虚拟机 182 9.5 写程序的程序 186 ...
  • 8.6 字节序 161 8.7 可移植性和升级 164 8.8 国际化 165 8.9 小结 167 第9章 记法 169 9.1 数据格式 169 9.2 正则表达式 174 9.3 可编程工具 180 9.4 解释器、编译器和虚拟机 182 9.5 写程序的程序 186 ...
  • 8.6 字节序 161 8.7 可移植性和升级 164 8.8 国际化 165 8.9 小结 167 第9章 记法 169 9.1 数据格式 169 9.2 正则表达式 174 9.3 可编程工具 180 9.4 解释器、编译器和虚拟机 182 9.5 写程序的程序 186 ...
  • 8.6 字节序 161 8.7 可移植性和升级 164 8.8 国际化 165 8.9 小结 167 第9章 记法 169 9.1 数据格式 169 9.2 正则表达式 174 9.3 可编程工具 180 9.4 解释器、编译器和虚拟机 182 9.5 写程序的程序 186 9.6 用宏生成...
  • 8.6 字节序 161 8.7 可移植性和升级 164 8.8 国际化 165 8.9 小结 167 第9章 记法 169 9.1 数据格式 169 9.2 正则表达式 174 9.3 可编程工具 180 9.4 解释器、编译器和虚拟机 182 9.5 写程序的程序 186 9.6 用宏生成...
  • c++ 面试题 总结

    2009-09-16 08:44:40
    块式管理:把主存分为一大块、一大块的,当所需的程序片断不在主存时就分配一块主存空间,把程 片断load入主存,就算所需的程序片度只有几个字节也只能把这一块分配给它。这样会造成很大的浪费,平均浪费了50%的...
  • -------------------------------------------- 主 程 ------------------------------------- MAIN: ;主程序 CLR ALB ;清除闹铃标志 ACALL BZ ;压电喇叭连续两次鸣响一声 ACALL BZ ACALL LED_SD ;LED ...
  • WinRAR_5.60_Beta1_x64_SC

    2018-04-16 16:28:31
    在“高级自解压选项/设置”对话框中也可以通过“退出代码调整”选项进行 访问。 10.新增“显示更多信息”WinRAR 命令行 -im 开关。 它可以与“t”命令一起使用,以便在成功压缩测试结果的情况下发出消息。 如果没有...
  • .1.6.1 给c程序员的建议 13 1.6.2 给c++程序员的建议 13 1.7 有关在c++里编程的思考 14 1.8 忠告 15 1.9 参考文献 16 第2章 c++概览 19 2.1 为什么是c++ 19 2.2 程序设计范型 19 2.3 过程式程序设计 20 ...
  • .1.6.1 给c程序员的建议 13 1.6.2 给c++程序员的建议 13 1.7 有关在c++里编程的思考 14 1.8 忠告 15 1.9 参考文献 16 第2章 c++概览 19 2.1 为什么是c++ 19 2.2 程序设计范型 19 2.3 过程式程序设计 20 ...

空空如也

空空如也

1 2 3
收藏数 45
精华内容 18
关键字:

c调整字节序