Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling C char arrays with Java char arrays
    primarykey
    data
    text
    <p>Through a socket I am sending information from a program written in C to a program written in Java.</p> <p>Through the program in C, I am sending two bytes through a char array (using an internet socket), and the received information in Java is stored in a char array also.</p> <p>My main problem is that the received information in the Java array does not correspond properly to the transmitted information from the C program.</p> <p>I have read that the chars in Java are 16 bits long, and the chars in C are 8 bits long. That may be the problem, but I do not know how to handle/solve that.</p> <p>The C code to send information is the following:</p> <pre><code>char buffer[256]; bzero(buffer,256); n = read(fd, buffer, 255); // getting info from an uart port, which works properly n = write(sockfd,buffer,3); // send the information through the socket </code></pre> <p>Part of the Java code (for an Android app) is the following:</p> <pre><code>char[] buffer = new char[256]; BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); int readX = in.read(buffer, 0, 3); if (readX &gt; 0) { // I am using a handler to manipulate the info Message msg = new Message(); msg.obj = buffer; mHandler.sendMessage(msg); } .... // Part of the handler is the following: mHandler = new Handler() { @Override public void handleMessage(Message msg) { char[] buffer; buffer = (char[])msg.obj; // the information here is different from the one sent from the C program .... } } </code></pre> <p>Any suggestion to solve this problem I would really appreciate it. </p> <p>Thanks in advance, Gus. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload