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.

4
  • 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. Commented Dec 7, 2014 at 9:34
  • 1
    @Raedwald if this is text at all and not binary data; OP doesn't tell Commented Dec 7, 2014 at 9:36
  • OK, so what about you told us exactly what you want to do? This looks like an XY problem to me Commented Dec 7, 2014 at 9:46
  • I want to passing byte stream from C program to JAVA program, InputStream solve my problem. Thank you! @fge Commented Dec 7, 2014 at 10:41

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.