简体   繁体   中英

Convert String received from C to Java String

I would like to convert a string that I send out from C, and receive it as a string value in Java.

Here is my code for sending string in C.

strcpy(buffer,"hello");
send(clientSocket,buffer,4096,0);

And my code to receive string in Java.

BufferedReader in = new BufferedReader(new InputStreamReader(LoginActivity.getSocket().getInputStream()));
String str = in.readLine();

However, when I do a toast, the str value comes out in weird symbols. How do i solve this?

You should change

send(clientSocket,buffer,4096,0); 

to

send(clientSocket,buffer,strlen(buffer)+1,0);

In this way you send only your string, not the whole buffer.

Moreover I think that

in.readLine()

hangs expecting for a newline char that isn't string sent.

您应该使用InputStreamReader(InputStream in, String charsetName)构造函数版本,并传递用于在C中创建文本的charsetName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM