| | |
Socket passing double type value from c++ to java
![]() |
•
•
Join Date: Jul 2009
Posts: 3
Reputation:
Solved Threads: 0
Hi,
I have some problems aka how to bring value of type double (or even array of double values) through socket from C++ server to Java applet client.
I tried two possibilities. For example:
Domen
I have some problems aka how to bring value of type double (or even array of double values) through socket from C++ server to Java applet client.
I tried two possibilities. For example:
- Doesn't work, but I think it could be ok while working
In c++ my server does:
double h=123.456; send(j,(const char*)&h,sizeof(h),0);
than i try to bring that double value in Java, but I'm really cofused, how.
And here is the problem, that I cannot bring that chars back to double in Java.Java Syntax (Toggle Plain Text)- BufferedReader in=new BufferedReader(new InputStreamReader(sock.getInputStream()));
- char[] buf=new char[8];
- i=in.read(buf); //so i get chars in char array
In c++ client (which I also use in S-function Matlab) is just easy with using memcpy and that's it! - Works, but is slow, and loose information
Than I decided to do otherwise, so in C++ i use sprintf(...) to bring double to int and int to char array, send char array to Java and than parse char array back to int and int to double, which could still be ok. But....i think it costs much more on CPU performance.
And parsing in java:char msg[10]; double h=123.456; int i=sprintf(msg,"%d",(int)(h*1000));//i will divide it back in java send(j,(const char*)&msg,i,0);
Java Syntax (Toggle Plain Text)- char[] buf=new char[10];
- String tempArr[]=new String[2];
- String tempStr;
- double a;
- in.read(buf);
- tempStr=new String(buf);
- tempArr=tempStr.split(";");
- a=((double)Integer.parseInt(tempArr[0]))/1000;
Domen
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 145
0
#2 Oct 10th, 2009
I wouldn't waste a single millisecond worrying about char/Stringdouble conversion CPU usage. Whatever you do will be a fraction of 1% of the CPU used by the OS doing the socket operations.
You don't need to do the conversion to int and back; just send the double in text format and parseDouble(...) at the Java end.
You don't need to do the conversion to int and back; just send the double in text format and parseDouble(...) at the Java end.
0
#3 Oct 10th, 2009
> You don't need to do the conversion to int and back; just send the
> double in text format and parseDouble(...) at the Java end.
I don't think that would handle values like +INF, -INF and NaN or even deal with the obvious truncation of values [think 1/3].
@Domenzup
Even though it might be a bit more work for you, your best bet would to rely on a data transfer format/specification which does the job of converting the double to a string and back *without* any loss and in a portable manner. Using a in-house hacked version to transfer data is something I won't recommend.
If you need an enterprisy solution, go with XML. JSON is another option which is fast along with being portable. Of course there are many other data transfer formats out there like ProtoBuffer but these are most widely used ones.
For libraries which deal with converting C++ to and from JSON to Java, see here.
> double in text format and parseDouble(...) at the Java end.
I don't think that would handle values like +INF, -INF and NaN or even deal with the obvious truncation of values [think 1/3].
@Domenzup
Even though it might be a bit more work for you, your best bet would to rely on a data transfer format/specification which does the job of converting the double to a string and back *without* any loss and in a portable manner. Using a in-house hacked version to transfer data is something I won't recommend.
If you need an enterprisy solution, go with XML. JSON is another option which is fast along with being portable. Of course there are many other data transfer formats out there like ProtoBuffer but these are most widely used ones.
For libraries which deal with converting C++ to and from JSON to Java, see here.
I don't accept change; I don't deserve to live.
•
•
Join Date: Jul 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#4 Oct 12th, 2009
Thanx very much for so quick reply. Now I decided to make one msg, in which I'll memcpy structure of double values for c++ client and in the same message after I attached values for java client with sprintf function, all separated with ";" for parsing.
Is not optimal solution, but I lost to much energy with searching how to read double value send from c++ to java that I gave up.
Domen
Is not optimal solution, but I lost to much energy with searching how to read double value send from c++ to java that I gave up.
Domen
![]() |
Similar Threads
- java.sql.SQLException: Invalid column type: 1111 (Java)
- what can i use to type decimals in java? (Java)
- Java Floating point problem (Java)
- From beginner in Java: Atomic type-what is it? (Java)
- Validating a double type variable to ensure it is an integer (C)
- Having trouble passing bool type to function....need help (C++)
- Question about read method of InputStream (Java)
Other Threads in the Java Forum
- Previous Thread: decimal to hexadecimal
- Next Thread: executing a .java file from another .java file
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card chat class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting scanner se server service set sms socket software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows






