MTC Developer
MTC Developer

Reputation: 11

Sockets: Passing unsigned char array from C to JAVA

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.

Upvotes: 0

Views: 816

Answers (1)

fge
fge

Reputation: 121840

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.

Upvotes: 3

Related Questions