Learning2code
Learning2code

Reputation: 51

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?

Upvotes: 1

Views: 168

Answers (2)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136122

You should use InputStreamReader(InputStream in, String charsetName) constructor version and pass charsetName used to create text in C

Upvotes: 0

LPs
LPs

Reputation: 16243

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.

Upvotes: 2

Related Questions