因项目需要用java.nio.ByteBuffer,更确切地说,是DirectBuffer作为Java-c/c++之间的数据传输载体,然后就被大小尾端的问题折腾了好久。。

ByteBuffer中可以在Java端设置当前Buffer的大/小尾端属性,默认是大尾端的,但修改该属性返回的新的ByteBuffer只是修改了该类的一个成员值,并不对其中存放的实际数据做任何改变,所以在没有发现有新的方法前,只能在Native端用bytearray存放ByteBuffer中的数据,然后逐一转换了。。

各种数据格式的转换参见以下代码。至于bytearray到float/double类型的转换,会涉及到reinterpret_cast,有待进一步研究~~

Ref:http://fire11.iteye.com/blog/1029925

 
  
  1. public class ByteConvert { 
  2.     // 以下 是整型数 和 网络字节序的  byte[] 数组之间的转换 
  3.     public static byte[] longToBytes(long n) { 
  4.         byte[] b = new byte[8]; 
  5.         b[7] = (byte) (n & 0xff); 
  6.         b[6] = (byte) (n >> 8  & 0xff); 
  7.         b[5] = (byte) (n >> 16 & 0xff); 
  8.         b[4] = (byte) (n >> 24 & 0xff); 
  9.         b[3] = (byte) (n >> 32 & 0xff); 
  10.         b[2] = (byte) (n >> 40 & 0xff); 
  11.         b[1] = (byte) (n >> 48 & 0xff); 
  12.         b[0] = (byte) (n >> 56 & 0xff); 
  13.         return b; 
  14.     } 
  15.      
  16.     public static void longToBytes( long n, byte[] array, int offset ){ 
  17.         array[7+offset] = (byte) (n & 0xff); 
  18.         array[6+offset] = (byte) (n >> 8 & 0xff); 
  19.         array[5+offset] = (byte) (n >> 16 & 0xff); 
  20.         array[4+offset] = (byte) (n >> 24 & 0xff); 
  21.         array[3+offset] = (byte) (n >> 32 & 0xff); 
  22.         array[2+offset] = (byte) (n >> 40 & 0xff); 
  23.         array[1+offset] = (byte) (n >> 48 & 0xff); 
  24.         array[0+offset] = (byte) (n >> 56 & 0xff); 
  25.     } 
  26.      
  27.     public static long bytesToLong( byte[] array ) 
  28.     { 
  29.         return ((((long) array[ 0] & 0xff) << 56
  30.               | (((long) array[ 1] & 0xff) << 48
  31.               | (((long) array[ 2] & 0xff) << 40
  32.               | (((long) array[ 3] & 0xff) << 32
  33.               | (((long) array[ 4] & 0xff) << 24
  34.               | (((long) array[ 5] & 0xff) << 16
  35.               | (((long) array[ 6] & 0xff) << 8)  
  36.               | (((long) array[ 7] & 0xff) << 0));         
  37.     } 
  38.      
  39.     public static long bytesToLong( byte[] array, int offset ) 
  40.     { 
  41.         return ((((long) array[offset + 0] & 0xff) << 56
  42.               | (((long) array[offset + 1] & 0xff) << 48
  43.               | (((long) array[offset + 2] & 0xff) << 40
  44.               | (((long) array[offset + 3] & 0xff) << 32
  45.               | (((long) array[offset + 4] & 0xff) << 24
  46.               | (((long) array[offset + 5] & 0xff) << 16
  47.               | (((long) array[offset + 6] & 0xff) << 8)  
  48.               | (((long) array[offset + 7] & 0xff) << 0));             
  49.     } 
  50.      
  51.     public static byte[] intToBytes(int n) { 
  52.         byte[] b = new byte[4]; 
  53.         b[3] = (byte) (n & 0xff); 
  54.         b[2] = (byte) (n >> 8 & 0xff); 
  55.         b[1] = (byte) (n >> 16 & 0xff); 
  56.         b[0] = (byte) (n >> 24 & 0xff); 
  57.         return b; 
  58.     } 
  59.      
  60.     public static void intToBytes( int n, byte[] array, int offset ){ 
  61.         array[3+offset] = (byte) (n & 0xff); 
  62.         array[2+offset] = (byte) (n >> 8 & 0xff); 
  63.         array[1+offset] = (byte) (n >> 16 & 0xff); 
  64.         array[offset] = (byte) (n >> 24 & 0xff); 
  65.     }     
  66.  
  67.     public static int bytesToInt(byte b[]) { 
  68.         return    b[3] & 0xff  
  69.                | (b[2] & 0xff) << 8  
  70.                | (b[1] & 0xff) << 16 
  71.                | (b[0] & 0xff) << 24
  72.     } 
  73.  
  74.     public static int bytesToInt(byte b[], int offset) { 
  75.         return    b[offset+3] & 0xff  
  76.                | (b[offset+2] & 0xff) << 8  
  77.                | (b[offset+1] & 0xff) << 16 
  78.                | (b[offset] & 0xff) << 24
  79.     } 
  80.  
  81.     public static byte[] uintToBytes( long n ) 
  82.     { 
  83.         byte[] b = new byte[4]; 
  84.         b[3] = (byte) (n & 0xff); 
  85.         b[2] = (byte) (n >> 8 & 0xff); 
  86.         b[1] = (byte) (n >> 16 & 0xff); 
  87.         b[0] = (byte) (n >> 24 & 0xff); 
  88.          
  89.         return b; 
  90.     } 
  91.  
  92.     public static void uintToBytes( long n, byte[] array, int offset ){ 
  93.         array[3+offset] = (byte) (n ); 
  94.         array[2+offset] = (byte) (n >> 8 & 0xff); 
  95.         array[1+offset] = (byte) (n >> 16 & 0xff); 
  96.         array[offset]   = (byte) (n >> 24 & 0xff); 
  97.     } 
  98.  
  99.     public static long bytesToUint(byte[] array) {   
  100.         return ((long) (array[3] & 0xff))   
  101.              | ((long) (array[2] & 0xff)) << 8   
  102.              | ((long) (array[1] & 0xff)) << 16   
  103.              | ((long) (array[0] & 0xff)) << 24;   
  104.     } 
  105.  
  106.     public static long bytesToUint(byte[] array, int offset) {    
  107.         return ((long) (array[offset+3] & 0xff))   
  108.               | ((long) (array[offset+2] & 0xff)) << 8   
  109.              | ((long) (array[offset+1] & 0xff)) << 16   
  110.              | ((long) (array[offset]   & 0xff)) << 24;   
  111.     } 
  112.  
  113.     public static byte[] shortToBytes(short n) { 
  114.         byte[] b = new byte[2]; 
  115.         b[1] = (byte) ( n       & 0xff); 
  116.         b[0] = (byte) ((n >> 8) & 0xff); 
  117.         return b; 
  118.     } 
  119.      
  120.     public static void shortToBytes(short n, byte[] array, int offset ) {         
  121.         array[offset+1] = (byte) ( n       & 0xff); 
  122.         array[offset] = (byte) ((n >> 8) & 0xff); 
  123.     } 
  124.      
  125.     public static short bytesToShort(byte[] b){ 
  126.         return (short)( b[1] & 0xff 
  127.                       |(b[0] & 0xff) << 8 );  
  128.     }     
  129.  
  130.     public static short bytesToShort(byte[] b, int offset){ 
  131.         return (short)( b[offset+1] & 0xff 
  132.                       |(b[offset]    & 0xff) << 8 );  
  133.     } 
  134.  
  135.     public static byte[] ushortToBytes(int n) { 
  136.         byte[] b = new byte[2]; 
  137.         b[1] = (byte) ( n       & 0xff); 
  138.         b[0] = (byte) ((n >> 8) & 0xff); 
  139.         return b; 
  140.     }     
  141.  
  142.     public static void ushortToBytes(int n, byte[] array, int offset ) { 
  143.         array[offset+1] = (byte) ( n       & 0xff); 
  144.         array[offset] = (byte)   ((n >> 8) & 0xff); 
  145.     } 
  146.  
  147.     public static int bytesToUshort(byte b[]) { 
  148.         return    b[1] & 0xff  
  149.                | (b[0] & 0xff) << 8
  150.     }     
  151.  
  152.     public static int bytesToUshort(byte b[], int offset) { 
  153.         return    b[offset+1] & 0xff  
  154.                | (b[offset]   & 0xff) << 8
  155.     }     
  156.  
  157.     public static byte[] ubyteToBytes( int n ){ 
  158.         byte[] b = new byte[1]; 
  159.         b[0] = (byte) (n & 0xff); 
  160.         return b; 
  161.     } 
  162.  
  163.     public static void ubyteToBytes( int n, byte[] array, int offset ){ 
  164.         array[0] = (byte) (n & 0xff); 
  165.     } 
  166.  
  167.     public static int bytesToUbyte( byte[] array ){             
  168.         return array[0] & 0xff
  169.     }         
  170.  
  171.     public static int bytesToUbyte( byte[] array, int offset ){             
  172.         return array[offset] & 0xff
  173.     }     
  174.     // char 类型、 float、double 类型和 byte[] 数组之间的转换关系还需继续研究实现。  
  175.  
  176.  
  177. 测试程序如下: 
  178.  
  179. public class ByteConvertTest { 
  180.      
  181.     public static String byte2Hex(byte[] buf)  
  182.     { 
  183.         StringBuffer strbuf = new StringBuffer(); 
  184.         strbuf.append("{"); 
  185.         for (byte b : buf)  
  186.         { 
  187.             if (b == 0)  
  188.             { 
  189.                 strbuf.append("00"); 
  190.             }  
  191.             else if (b == -1)  
  192.             { 
  193.                 strbuf.append("FF"); 
  194.             }  
  195.             else  
  196.             { 
  197.                 String str = Integer.toHexString(b).toUpperCase(); 
  198.                 // sb.append(a); 
  199.                 if (str.length() == 8)  
  200.                 { 
  201.                     str = str.substring(68); 
  202.                 }  
  203.                 else if (str.length() < 2)  
  204.                 { 
  205.                     str = "0" + str; 
  206.                 } 
  207.                 strbuf.append(str); 
  208.             } 
  209.             strbuf.append(" "); 
  210.         } 
  211.         strbuf.append("}"); 
  212.         return strbuf.toString(); 
  213.     }     
  214.  
  215.     public static byte[] longToBytes(long n) { 
  216.         byte[] b = new byte[8]; 
  217.         b[7] = (byte) (n & 0xff); 
  218.         b[6] = (byte) (n >> 8  & 0xff); 
  219.         b[5] = (byte) (n >> 16 & 0xff); 
  220.         b[4] = (byte) (n >> 24 & 0xff); 
  221.         b[3] = (byte) (n >> 32 & 0xff); 
  222.         b[2] = (byte) (n >> 40 & 0xff); 
  223.         b[1] = (byte) (n >> 48 & 0xff); 
  224.         b[0] = (byte) (n >> 56 & 0xff); 
  225.         return b; 
  226.     } 
  227.  
  228.     public static long bytesToLong( byte[] array ) 
  229.     { 
  230.         return ((((long) array[ 0] & 0xff) << 56
  231.               | (((long) array[ 1] & 0xff) << 48
  232.               | (((long) array[ 2] & 0xff) << 40
  233.               | (((long) array[ 3] & 0xff) << 32
  234.               | (((long) array[ 4] & 0xff) << 24
  235.               | (((long) array[ 5] & 0xff) << 16
  236.               | (((long) array[ 6] & 0xff) << 8)  
  237.               | (((long) array[ 7] & 0xff) ));         
  238.     } 
  239.      
  240.     public static int bytesToInt(byte b[]) { 
  241.         return    b[3] & 0xff  
  242.                | (b[2] & 0xff) << 8  
  243.                | (b[1] & 0xff) << 16 
  244.                | (b[0] & 0xff) << 24
  245.     } 
  246.  
  247.     public static long bytesToUint(byte[] array) {   
  248.         return ((long) (array[3] & 0xff))   
  249.              | ((long) (array[2] & 0xff)) << 8   
  250.              | ((long) (array[1] & 0xff)) << 16   
  251.              | ((long) (array[0] & 0xff)) << 24;   
  252.     } 
  253.  
  254.     public static byte[] uintToBytes( long n ) 
  255.     { 
  256.         byte[] b = new byte[4]; 
  257.         b[3] = (byte) (n & 0xff); 
  258.         b[2] = (byte) (n >> 8 & 0xff); 
  259.         b[1] = (byte) (n >> 16 & 0xff); 
  260.         b[0] = (byte) (n >> 24 & 0xff); 
  261.          
  262.         return b; 
  263.     } 
  264.      
  265.  
  266.     public static byte[] shortToBytes(short n) { 
  267.         byte[] b = new byte[2]; 
  268.         b[1] = (byte) ( n       & 0xff); 
  269.         b[0] = (byte) ((n >> 8) & 0xff); 
  270.         return b; 
  271.     } 
  272.      
  273.     public static short bytesToShort(byte[] b){ 
  274.         return (short)( b[1] & 0xff 
  275.                       |(b[0] & 0xff) << 8 );  
  276.     } 
  277.      
  278.     static void testShortConvert(){ 
  279.         System.out.println("=================== short convert ============="); 
  280.         System.out.println("byte2Hex(shortToBytes((short)0x11f2))"+byte2Hex(shortToBytes((short)0x11f2)));         
  281.         System.out.print("println 0x11f2:"); 
  282.         System.out.println((short)0x11f2);         
  283.         System.out.println("byte2Hex(shortToBytes((short)0xf1f2))"+byte2Hex(shortToBytes((short)0xf1f2)));         
  284.         System.out.print("println 0xf1f2:"); 
  285.         System.out.println((short)0xf1f2);             
  286.         System.out.print("println bytesToShort(shortToBytes((short)0x11f2)):"); 
  287.         System.out.println((short)bytesToShort(shortToBytes((short)0x11f2)));             
  288.         System.out.print("println bytesToShort(shortToBytes((short)0xf1f2)):"); 
  289.         System.out.println((short)bytesToShort(shortToBytes((short)0xf1f2)));         
  290.     } 
  291.      
  292.  
  293.     public static byte[] ushortToBytes(int n) { 
  294.         byte[] b = new byte[2]; 
  295.         b[1] = (byte) (n & 0xff); 
  296.         b[0] = (byte) (n >> 8 & 0xff); 
  297.         return b; 
  298.     } 
  299.      
  300.  
  301.     public static int bytesToUshort(byte b[]) { 
  302.         return    b[1] & 0xff  
  303.                | (b[0] & 0xff) << 8
  304.     } 
  305.  
  306.     static void testUshortConvert(){ 
  307.         System.out.println("=================== Ushort convert ============="); 
  308.         System.out.println("byte2Hex(ushortToBytes(0x11f2))"+byte2Hex(ushortToBytes(0x11f2)));         
  309.         System.out.print("println 0x11f2:"); 
  310.         System.out.println(0x11f2);         
  311.         System.out.println("byte2Hex(ushortToBytes(0xf1f2))"+byte2Hex(ushortToBytes(0xf1f2)));         
  312.         System.out.print("println 0xf1f2:"); 
  313.         System.out.println(0xf1f2);             
  314.         System.out.print("println bytesToUshort(ushortToBytes(0x11f2)):"); 
  315.         System.out.println(bytesToUshort(ushortToBytes(0x11f2)));             
  316.         System.out.print("println bytesToUshort(ushortToBytes(0xf1f2)):"); 
  317.         System.out.println(bytesToUshort(ushortToBytes(0xf1f2)));         
  318.     } 
  319.      
  320.     public static byte[] ubyteToBytes( int n ){ 
  321.         byte[] b = new byte[1]; 
  322.         b[0] = (byte) (n & 0xff); 
  323.         return b; 
  324.     } 
  325.  
  326.     public static int bytesToUbyte( byte[] array ){             
  327.         return array[0] & 0xff
  328.     }     
  329.  
  330.     static void testUbyteConvert(){ 
  331.         System.out.println("=================== Ubyte convert ============="); 
  332.         System.out.println("byte2Hex(ubyteToBytes(0x1112))"+byte2Hex(ubyteToBytes(0x1112)));         
  333.         System.out.print("println 0x1112:"); 
  334.         System.out.println(0x1112);         
  335.         System.out.println("byte2Hex(ubyteToBytes(0xf2))"+byte2Hex(ubyteToBytes(0xf2)));         
  336.         System.out.print("println 0xf2:"); 
  337.         System.out.println(0xf2);             
  338.         System.out.print("println bytesToUbyte(ubyteToBytes(0x1112)):"); 
  339.         System.out.println(bytesToUbyte(ubyteToBytes(0x1112)));             
  340.         System.out.print("println bytesToUbyte(ubyteToBytes(0xf1f2)):"); 
  341.         System.out.println(bytesToUbyte(ubyteToBytes(0xf1f2)));         
  342.     } 
  343.      
  344.      
  345.     /** 
  346.      * @param args 
  347.      */ 
  348.     public static void main(String[] args) { 
  349.         // TODO Auto-generated method stub         
  350.         byte[] array = new byte[4]; 
  351.         array[3] = (byte0xF4
  352.         array[2] = 0x13
  353.         array[1] = 0x12
  354.         array[0] = 0x11
  355.          
  356.         System.out.println("=================== Integer bytes ============="); 
  357.          
  358.         System.out.println("the bytes is:"+byte2Hex(array) ); 
  359.         System.out.print("println bytesToInt :"); 
  360.         System.out.println( bytesToInt(array)); 
  361.         System.out.printf("printf bytesToInt :%X\n", bytesToInt(array)); 
  362.          
  363.         System.out.println("=================== long bytes ============="); 
  364.         byte[] longBytes = new byte[8]; 
  365.          
  366.         longBytes[7] = (byte0xf7
  367.         longBytes[6] = (byte0x16
  368.         longBytes[5] = (byte0xf5
  369.         longBytes[4] = (byte0x14
  370.         longBytes[3] = (byte0xf3
  371.         longBytes[2] = (byte0x12
  372.         longBytes[1] = (byte0xf1
  373.         longBytes[0] = (byte0x10
  374.          
  375.  
  376.         System.out.println( "the bytes is:"+byte2Hex(longBytes) ); 
  377.         System.out.printf("printf bytesToLong:%X\n",bytesToLong(longBytes)); 
  378.          
  379.         System.out.println("=================byte to long ================"); 
  380.          
  381.         byte b = (byte)0xf1
  382.         System.out.print("Println the byte:"); 
  383.         System.out.println(b); 
  384.         System.out.printf("Printf the byte:%X\n",b); 
  385.         long l = b; 
  386.         System.out.print("Println byte to long:"); 
  387.         System.out.println(l); 
  388.         System.out.printf("printf byte to long:%X\n",l); 
  389.          
  390.         System.out.println("================= uint Bytes ================"); 
  391.          
  392.         byte[] uint = new byte[4]; 
  393.         uint[3] = (byte0xf3
  394.         uint[2] = (byte0x12
  395.         uint[1] = (byte0xf1
  396.         uint[0] = (byte0xFF
  397.          
  398.         System.out.println( "the bytes is:"+byte2Hex(uint) ); 
  399.         System.out.printf("printf bytesToUint:%X\n",bytesToUint(uint)); 
  400.         System.out.print("Println bytesToUint:"); 
  401.         System.out.println(bytesToUint(uint)); 
  402.         System.out.println("byte2Hex(uintToBytes(0x11f2f3f4f5f6f7f8l)):"+byte2Hex(uintToBytes(0x11f2f3f4f5f6f7f8l))); 
  403.          
  404.         System.out.println("===============Long Integer==============");         
  405.         System.out.print("println 0x11f2f3f4f5f6f7f8l:"); 
  406.         System.out.println(0x11f2f3f4f5f6f7f8l);         
  407.         System.out.printf("Printf 0x11f2f3f4f5f6f7f8l:%X\n",0x11f2f3f4f5f6f7f8l); 
  408.         System.out.println("println byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l))"+byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l))); 
  409.         // 注意,下面的这行,并不能获得正确的uint。 
  410.         System.out.printf("printf bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l):%X\n",bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l))); 
  411.          
  412.         System.out.println("===============bytesToLong(longToBytes())=============="); 
  413.         System.out.println(bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l))); 
  414.         System.out.printf("%X\n",bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l))); 
  415.          
  416.         testShortConvert(); 
  417.         testUshortConvert(); 
  418.         testUbyteConvert(); 
  419.     } 
  420.