Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interpreting Java binaries to C/C++

Status
Not open for further replies.

Zech

Programmer
Oct 14, 2002
116
US
Hi,

I have a server program that is going to be written in C/C++ and some clients written in Java. The client programs are going to send and receive binary data from the server. Now my problem is with interpreting Java binaries to something that C/C++ can understand.

I understand that Java data types are somewhat longer than C/C++ data types and that Java data types may require further interpretation by JVM.

I wonder whether anyone has done this before and whether anyone has a good solution to this problem. Thanks.

Zech
 

The lengths of Java data types are fixed regardless of the OS and architecture whereas the lengths of C/C++ data types vary depending on the OS, architecture, and compiler implementation.

For instance, these are the lengths of Java's basic data types:

boolean: 2 bytes
char: 2 bytes
double: 8 bytes
float: 4 bytes
int: 4 bytes
long: 8 bytes
short: 2 bytes

And these are the lengths of C/C++'s basic data types on my machine (WinXP, IBM-compatible PC)

boolean: 1 byte
char: 1 byte
double: 8 bytes
float: 4 bytes
int: 4 bytes
long: 4 bytes
short: 2 bytes

Due to these differences, I suppose that Java must have its own basic data types binary implementation. That's why I started the thread to see if anyone knows how Java implements its data types.

Zech
 
Depends if it is ANSI C or not ...
 
When you exchange data over the net, you will have to define how you send your data, and then you will now, how to receive them.
Most methods use byte[] to send and receive, and then it's up to you to send a boolean as 2 bytes, one byte or as single bit.

seeking a job as java-programmer in Berlin:
 
My main problem is the differences in the binary implementation of Java and C/C++, and not the lengths of those data types. The differences in their lengths are indications of Java's independent binary implementation.

This means the binary of Java's 4-bytes float may not be the same as the binary of C/C++'s 4-bytes float (although they may be of the same length). Thus, I cannot just cut Java's 2-bytes char into 1 byte, and assume that C/C++ would read it properly as its own native char.

Somebody in the C forum pointed out the possible use of CORBA, which I think is pretty interesting. Alternate solutions on my list right now are the use of text-based data transmission instead of binary, and the use of third-party C/C++ library to read Java's binaries. I am also considering the use of JNI for the server implementation. Hopefully, I can find a simpler way to solve the problem.
 
Did you test write/read of 4-bytes-floats?

If the data-amount isn't very high, and the performance needn't be highspeed, I would consider transforming the data to and from Strings ("-7 -41.437 42 34344 foo").

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top