服务器:C++
客户端:JAVA
在JAVA端获得InputStream之后,把十六进制数据ab 61 56 40 8f ae 73 da 1b 60 3a 1f 2d 59 ae 93转换成字节数组后通过InputStream发出,然而C++端收到数据确是ff 61 56 40 ff ff 73 ff ff 60 ff ff ff 59 ff 93,请问大虾们这是什么原因啊,是因为JAVA中BYTE就-128到127的原因吗?在线等,谢过各位大虾了。。
十六进制转字节数组如下:
/* * 把16进制字符串转换成字节数组
     * @param hex
     * @return
     */
    public static byte[] hexStringToByte(String hex) {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }
        return result;
    }

解决方案 »

  1.   

    应该是两边的编码格式不一样,你在java端写入到流中之前设置一下编码,在c++端接受前也设置一下编码,两边编码保持一致。
      

  2.   

    我做过跟C通讯的,应该跟C++一样
    要注意一下字节序,java是高低位存放字节数组的,而c++是从低位到高位的
    对于short,int这些非单字节的类型在交互的时候需要转换一下字节序
      

  3.   

    你可以参考一下这个,里面还有转换的源码
    希望对你有用
    http://baike.baidu.com/view/2194385.htm
      

  4.   

    你可以参考一下这个,里面还有转换的源码
    希望对你有用
    http://baike.baidu.com/view/2194385.htm
      

  5.   

    可以用  apache 的 axis 2  生成 wsdl
       就 可以通信了 
      

  6.   

    两边都用byte格式的数据就可以
      

  7.   

    对,JAVA是UNICODE,别用十六进制,用二进制
      

  8.   

    public class UnsignedNumber
    {  // =============================================== bytecode to usigned number  /**
       *
       * @param b byte
       * @return short
       */
      public static short b_u1(byte b)
      {
        return (short)((b < 0) ? ((b & 0x7F) + 0x80) : b) ;
      }  /**
       *
       * @param b byte[]
       * @param offset int
       * @return short
       */
      public static short b_u1(byte b[], int offset)
      {
        return (short)((b[offset] < 0) ? ((b[offset] & 0x7F) + 0x80) : b[offset]) ;
      }  // =============================================== little-endian bytecode to usigned number
      // For example: Intel for Windows  /**
       *
       * @param b byte[]
       * @param offset int
       * @return short
       */
      public static int bb_u2(byte b[], int offset)
      {
        int int_a = ((b[offset] < 0) ? ((b[offset] & 0x7F) + 0x80) : b[offset]);
        int int_b = ((b[offset+1] < 0) ? ((b[offset+1] & 0x7F)+ 0x80) : b[offset+1]) * 0x0100 ;
        return int_a + int_b;
      }
      public static void setFileldAsString(String s, int index, int len, byte[] data) {
    if (s != null) {
    byte[] sb = s.getBytes();
    for (int i = 0; i < len; i++) {
    if (i < sb.length) {
    data[index + i] = sb[i];
    }
    }
    }
    }
      /**
       *
       * @param b byte[]
       * @param offset int
       * @return long
       */
      public static long bb_u4(byte b[], int offset)
      {
        long l_a = ((b[offset] < 0) ? ((b[offset] & 0x7F) + 0x80) : b[offset]);
        long l_b = ((b[offset+1] < 0) ? ((b[offset+1] & 0x7F)+ 0x80) : b[offset+1]) * 0x0100L ;
        long l_c = ((b[offset+2] < 0) ? ((b[offset+2] & 0x7F)+ 0x80) : b[offset+2]) * 0x010000L ;
        long l_d = ((b[offset+3] < 0) ? ((b[offset+3] & 0x7F)+ 0x80) : b[offset+3]) * 0x01000000L ;
        return l_a + l_b + l_c + l_d;
      }  // =============================================== big-endian bytecode to usigned number  /**
       *
       * @param b byte[]
       * @param offset int
       * @return short
       */
      public static int lb_u2(byte b[], int offset)
      {
        int int_a = ((b[offset+1] < 0) ? ((b[offset+1] & 0x7F) + 0x80) : b[offset+1]);
        int int_b = ((b[offset] < 0) ? ((b[offset] & 0x7F)+ 0x80) : b[offset]) * 0x0100 ;
        return int_a + int_b;
      }  /**
       *
       * @param b byte[]
       * @param offset int
       * @return long
       */
      public static long lb_u4(byte b[], int offset)
      {
        long l_a = ((b[offset+3] < 0) ? ((b[offset+3] & 0x7F) + 0x80) : b[offset+3]);
        long l_b = ((b[offset+2] < 0) ? ((b[offset+2] & 0x7F)+ 0x80) : b[offset+2]) * 0x0100L ;
        long l_c = ((b[offset+1] < 0) ? ((b[offset+1] & 0x7F)+ 0x80) : b[offset+1]) * 0x010000L ;
        long l_d = ((b[offset] < 0) ? ((b[offset] & 0x7F)+ 0x80) : b[offset]) * 0x01000000L ;
        return l_a + l_b + l_c + l_d;
      }  // =============================================== usigned number to bytecode  /**
       *
       * @param b byte[]
       * @param offset int
       * @param u1 int
       */
      public static void u1_b(byte b[], int offset, int u1)
      {
        b[offset] = (byte)u1;
      }  // =============================================== usigned number to littl-endian bytecode  /**
       *
       * @param b byte[]
       * @param offset int
       * @param u1 int
       */
      public static void u2_bb(byte b[], int offset, int u2)
      {
        b[offset] = (byte)u2;
        b[offset+1] = (byte)(u2 >>> 8);
      }  /**
       *
       * @param b byte[]
       * @param offset int
       * @param u1 int
       */
      public static void u4_bb(byte b[], int offset, long u4)
      {
        b[offset] = (byte)u4;
        b[offset+1] = (byte)(u4 >>> 8);
        b[offset+2] = (byte)(u4 >>> 16);
        b[offset+3] = (byte)(u4 >>> 24);
      }  // =============================================== usigned number to big-endian bytecode  /**
       *
       * @param b byte[]
       * @param offset int
       * @param u1 int
       */
      public static void u2_lb(byte b[], int offset, int u2)
      {
        b[offset] = (byte)(u2 >>> 8);
        b[offset+1] = (byte)u2;
      }  /**
       *
       * @param b byte[]
       * @param offset int
       * @param u1 int
       */
      public static void u4_lb(byte b[], int offset, long u4)
      {
        b[offset] = (byte)(u4 >>> 24);
        b[offset+1] = (byte)(u4 >>> 16);
        b[offset+2] = (byte)(u4 >>> 8);
        b[offset+3] = (byte)u4;
      }