0

C side:

unsigned char myBuffer[62];
fread(myBuffer,sizeof(char),62,myFile);
send(mySocket, myBuffer, 62,0);

JAVA side:

bufferedReader.read(tempBuffer,0,62);

Now in JAVA program i receive (using socket) values less than 0x80 in C program with no problems, but i receive 0xFD value for all values equal or greater than 0x80 in C program. I need perfect solution for this problem.

  • Java is more explicit than C about character encoding. You must know the character encoding that your C program uses, so your Java program interprets the bytes using the same encoding. – Raedwald Dec 07 '14 at 09:34
  • 1
    @Raedwald if this is text at all and not binary data; OP doesn't tell – fge Dec 07 '14 at 09:36
  • OK, so what about you told us exactly what you want to do? This looks like an [XY problem](https://xyproblem.info) to me – fge Dec 07 '14 at 09:46
  • I want to passing byte stream from C program to JAVA program, InputStream solve my problem. Thank you! @fge – MTC Developer Dec 07 '14 at 10:41

1 Answers1

3

Don't use a Reader to read bytes, use an InputStream!

A Reader is meant to read characters; it receives a stream of bytes and (tries and) converts these bytes to characters; you lose the original bytes.

In more details, a Reader will use a CharsetDecoder; this decoder is configured so that unknown byte sequences are replaced; and the encoding used here likely replaces unknown byte sequences with character 0x00fd, hence your result.

Also, you don't care about signed vs unsigned; 1000 0000 may be 128 as an unsigned char in C and -127 as a byte in Java, it still remains 1000 0000.


If what you send is really text, then it means the charset you have chosen for decoding is not the good one; you must know the encoding in which the files on your original system are written.

fge
  • 119,121
  • 33
  • 254
  • 329