现在的位置: 首页 > 综合 > 正文

在JAVA中使用Socket和C#通讯的解决

2014年12月03日 ⁄ 综合 ⁄ 共 2348字 ⁄ 字号 评论关闭

由于JAVA使用的byte是有符号类型,而C#(包括C++)中的byte是无符号的,因此,在收发byte[]时都要进行转换处理,研究了几小时,发表解决方案如下:

Java代码 复制代码
  1. public class Test {   
  2.   
  3.     /**  
  4.      * @param args  
  5.      */  
  6.     public static void main(String[] args) {   
  7.         //Send to C#   
  8.         byte[] a = Int2BytesLH(800);   
  9.            
  10.         //模拟Recv from C#   
  11.         byte[] b = new byte[]{32,3,0,0};   
  12.         int c = Bytes2Int(BytestoHL(b));   
  13.            
  14.         //Send to JAVA   
  15.         byte[] a1 = Int2Bytes(800);   
  16.            
  17.            
  18.         //模拟Recv from JAVA   
  19.         byte[] b1 = new byte[]{0,0,3,32};   
  20.         int c1 = Bytes2Int(b1);   
  21.            
  22.            
  23.     }   
  24.   
  25.        
  26.   
  27.     /**  
  28.      * 将一个Int 数据,转换为byte数组.  
  29.      * JAVA直接使用  
  30.      * @param intValue Int 数据  
  31.      * @return byte数组.  
  32.      */  
  33.     public static byte[] Int2Bytes(int intValue) {   
  34.      byte [] result = new byte[4];   
  35.      result[0] = (byte) ((intValue & 0xFF000000) >> 24);   
  36.      result[1] = (byte) ((intValue & 0x00FF0000) >> 16);   
  37.      result[2] = (byte) ((intValue & 0x0000FF00) >> 8);   
  38.      result[3] = (byte) ((intValue & 0x000000FF) );   
  39.      return result;   
  40.     }   
  41.   
  42.     /**  
  43.      * 将int转为低字节在前,高字节在后的byte数组  
  44.      * 转为C#需要的的数组顺序  
  45.      *   
  46.      */  
  47.     private static byte[] Int2BytesLH(int n) {   
  48.         byte[] b = new byte[4];   
  49.         b[0] = (byte) (n & 0xff);   
  50.         b[1] = (byte) (n >> 8 & 0xff);   
  51.         b[2] = (byte) (n >> 16 & 0xff);   
  52.         b[3] = (byte) (n >> 24 & 0xff);   
  53.         return b;   
  54.     }   
  55.   
  56.     /**  
  57.      * 将byte[]转为低字节在前,高字节在后的byte数组  
  58.      * 从C#收包后转换为JAVA的数组顺序  
  59.      */  
  60.     private static byte[] BytestoHL(byte[] n) {   
  61.         byte[] b = new byte[4];   
  62.         b[3] = n[0];   
  63.         b[2] = n[1];   
  64.         b[1] = n[2];   
  65.         b[0] = n[3];   
  66.         return b;   
  67.     }   
  68.   
  69.     /**  
  70.      * 将byte数组的数据,转换成Int值.  
  71.      * JAVA直接使用  
  72.      * @param byteVal byte数组  
  73.      * @return Int值.  
  74.      */  
  75.     public static int Bytes2Int(byte[] byteVal) {   
  76.      int result = 0;   
  77.      for(int i = 0; i < byteVal.length; i ++) {   
  78.       int tmpVal = (byteVal[i] << (8 * (3-i)));   
  79.       switch (i) {   
  80.        case 0:   
  81.         tmpVal = tmpVal & 0xFF000000;   
  82.         break;   
  83.        case 1:   
  84.         tmpVal = tmpVal & 0x00FF0000;   
  85.         break;   
  86.        case 2:   
  87.         tmpVal = tmpVal & 0x0000FF00;   
  88.         break;   
  89.        case 3:   
  90.         tmpVal = tmpVal & 0x000000FF;   
  91.         break;   
  92.       }   
  93.       result = result | tmpVal;   
  94.      }   
  95.      return result;   
  96.     }   
  97.   
  98. }  

抱歉!评论已关闭.