Issue Sending byte from C to java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • grapesan
    New Member
    • Feb 2010
    • 3

    Issue Sending byte from C to java

    I am sending integer from c to java .
    In C , integer is 24565. I converted it to 4 bytes. Its byte conversion is
    First Byte: -11
    Second Byte: 95
    Third Byte: 0
    Fourth Byte: 0.

    My C code is
    char content_size_ch ars[4];
    int content_size=24 565;

    // Convert output length to bytes
    content_size_ch ars[0] = content_size & 0xFF;
    content_size_ch ars[1] = (content_size & 0xFF00) >> 8;
    content_size_ch ars[2] = (content_size & 0xFF0000) >> 16;
    content_size_ch ars[3] = (content_size & 0xFF000000) >> 24;

    // write byte array to stderr stream
    fprintf(stderr, "%d",content_si ze_chars[0] );
    fprintf(stderr, "%d",content_si ze_chars[1] );
    fprintf(stderr, "%d",content_si ze_chars[2] );
    fprintf(stderr, "%d",content_si ze_chars[3] );
    fflush(stderr);


    In java, bytes read using datainputstream are

    byte []lengthBytes=new byte[4];
    lengthBytes[0]=dataInStream.r eadByte();
    lengthBytes[1]=dataInStream.r eadByte();
    lengthBytes[2]=dataInStream.r eadByte();
    lengthBytes[3]=dataInStream.r eadByte();

    Output is :
    First byte: -
    Second Byte: 1
    Third Byte: 1
    Fourth Byte: 9


    How to send integer from C to java ?

    Thanks
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    What is that mean sending data from c to java?

    Comment

    • grapesan
      New Member
      • Feb 2010
      • 3

      #3
      C is executed in separate process using Runtime.exec(). C writes data on stdout and stderr. Data should be send as header + data. Header is 4 byte length of data. So Java is reading stdout and stderr of the C process.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Try writing the integer as text. That is, 24565 comes out as "24565". Then Java can read the text.

        Comment

        • grapesan
          New Member
          • Feb 2010
          • 3

          #5
          Thanks. That works fine but commuincation between java and c is continuous. I mean C sends data as header(length of data) + data + header (length of data)+ ............ So header tells how much to read as data. For this we should know size of header which we decided as 4 bytes.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            OK, wrap a header around the text representation of the number.

            Comment

            Working...