if u notice that it is 32bit long, then i think you get the way to finish your job. hehe

解决方案 »

  1.   

    try: 
    convert int to long.
    String a = long1.toBinaryString();
      

  2.   

    up, 小弟是java的新手,请执教
      

  3.   

    String sTemp=null;
    sTemp=Integer.toString(command_id);
      

  4.   

    不知道是我没弄明白,还是我的意思没有表达清楚;
    程序的用途: 与中国移动的短信网关连接(CMPP),协议1.1
    小弟的VC的程序片断如下:// message header
    struct Message_Header{
    DWORD Total_Length;  //长度为4字节
    DWORD Command_ID;
    DWORD Sequence_ID;
    };// CMPP_Connect 请求连接
    struct CMPP_Connect_Struct{
    Message_Header MSGHead;
    char Source_Addr[6];              //30
    char AuthenticatorICP[16];
    char Reverse[8];    //保留
    };BOOL Send_CMPP_Connect_Request(SOCKET SocketID)
    {
    CMPP_Connect_Struct CMPP_Connect;
    int MsgLength,ret;
    char* SendDataBuf=NULL;
    char tmpStr[80]; if (SocketID < 0) return FALSE;
    MsgLength = sizeof(CMPP_Connect_Struct); // the message header's data
    CMPP_Connect.MSGHead.Total_Length = htonl(MsgLength);
    CMPP_Connect.MSGHead.Command_ID = htonl(0x00000001);
    MessageSequenceID++;
    CMPP_Connect.MSGHead.Sequence_ID =htonl(MessageSequenceID); // the message body's data
    strcpy(CMPP_Connect.Source_Addr,"888888");
    for (int i=1;i<16;i++)
    CMPP_Connect.AuthenticatorICP[i] = (char)(i);
    memset(CMPP_Connect.AuthenticatorICP,0,1);
    strcpy(CMPP_Connect.Reverse,"00000000"); // apply the save data place
             //=================================================
    SendDataBuf = (char *)(&CMPP_Connect);
             //-------------------------------------------------
    //send the data using the builded socket
    ret = send(SocketID,SendDataBuf,MsgLength,0);
    if ( ret > WSABASEERR )
    {
    WriteMsgList("SOCKET发送数据错误!");
    return FALSE;
    }
    // write Success msg to listbox
    memset(tmpStr,0,sizeof(tmpStr));
    sprintf(tmpStr,"%s%d","CMPP_Connect数据发送成功; Length:",MsgLength);
    WriteMsgList(tmpStr); return TRUE;  
    }程序中有===,---相夹的一句代码,就是将整个结构变成字符串,再通过SOCKET传送;
    现在小弟的问题是:
        1.在JAVA中有类似的操作方式吗?
        2.如果没有,请告诉我,如何将一个4BYTE长的INT转换成类似VC中00 00 00 xx的内存字符串形式,从而能够正确的通信.
        (小弟的VC程序已经能正确运行)
      

  5.   

    public static byte[] bwritedata(int data) 
       {
          byte[] bx = new byte[4];
          try 
          {
             DataOutputStream fin;
             ByteArrayOutputStream b = new ByteArrayOutputStream(4);
             fin = new DataOutputStream(b);
             fin.writeInt(data);
             fin.flush();
             fin.close();
             fin = null;
             bx = b.toByteArray();
          }
          catch(Exception e) 
          {
             log(" bAppending/writing int data error : " + e.toString());
          }
          return bx;
       }
      

  6.   

    try this:
    //int 32bits
    static void printBinInt(int i) {
        for (inti j = 31; j >= 0; j--)
            if ((1 << j) & i) != 0)
                System.out.print("1");
            else 
                System.out.print("0");
    }//long 64bits
    static void printBinLong(long l) {
        for (inti i = 63; i >= 0; i--)
            if ((1L << i) & l) != 0)
                System.out.print("1");
            else 
                System.out.print("0");
    }