• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

c to java data type

 
bruce truong
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to convert c to java but I am having difficult to convert c data types to java. This program reads a binary file and stores it in a common_block. The common_block is defined below.


Now I convert it into Java as such:


But when I try to read the binary file using DataInpuStream it does not come out correctly. Only type, key, and id were retrieved correctly. All other values do not match. Can someone points out what I am doing wrong?

Thank you in advance
bt
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure how the data types are stored in C, but the first thing that I notice is that you're using floats in Java where you had doubles in C. Shouldn't it be a double in Java as well? If you've got a variable of the wrong size it would explain why you'd get different results - not only would you get the wrong value for that variable but you'd get the wrong value for every following variable as well. I'm guessing that type, key, and id are the first three fields that are read?

Once you've got variables of the right size, you may still find some aren't right. It depends on the storage format of the variables - it wouldn't surprise me at all (but I don't know for sure) if C used a different floating point format to Java. If so, for those items you may need to fiddle about with the bits directly to get the right values.

[Moving to Java in General, because this isn't really a beginner's topic]
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also, you didn't show -- how the C code saved to the file, and how the Java loaded the binary file. And of course, what data you are getting, and how it is wrong.

Henry
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:I'm not sure how the data types are stored in C


They're not.

For example, widths are not mandated. All that matters in terms of size of a given type is something like short <= int <= long.

So depending on which compiler, hardware, and OS you use, you might end up with them being the same size as in Java, or you might end up with short, int, and long all 2 bytes, or you might end up with them all being 8 bytes, or various combinations in between.
 
Wendy Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why are you writing the file as a binary file? not just plain old text, no idea if this matters but just a thought.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DataInputStream and DataOutputStream are for reading and writing binary data in Java's serialization format. That format is not a straightforward binary dump of the variables in exactly the way as the struct would be laid out in memory in C.

So if you have some binary file in which the struct is stored in a C memory layout, it is not going to be possible to read it with DataInputStream in Java. Instead, you would have to use a normal FileInputStream and read it byte by byte, and assemble the data in your Java program.
 
bruce truong
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your prompt reply. I found out what's happening. It's like Jeff stated, it has to do with the hardware and OS. This program was written for UNIX and now I am working in Windows 7 64-bit. The machine bit architecture are different on each platform. It's high-order vs low-order bits. I appreciate all your comments.

bt
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bruce truong wrote:Thank you all for your prompt reply. I found out what's happening. It's like Jeff stated, it has to do with the hardware and OS. This program was written for UNIX and now I am working in Windows 7 64-bit. The machine bit architecture are different on each platform. It's high-order vs low-order bits. I appreciate all your comments.

bt


Take a look at the java.nio.ByteBuffer class. It has methods for various datatypes of various sizes -- along with handling of bit ordering (Big versus little Endian).

Henry
 
bruce truong
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I implemented it using Integer.reverseBytes() and it works fine. ByteBuffer class is too complicated for me but I will keep that suggestion in mind.

bt
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest, strongly, that you don't pass binary data. Convert the data to strings, and transmit it a comma separated values (CSV)

Otherwise, you can run into huge numbers of problems, from big endian vs little endian, ,different sizes of "char", etc.

If you must do it as binary, the only reliable way is to "marshal" the data, Google for CORBA and see how it marshals data -- its non-trivial.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:
If you must do it as binary, the only reliable way is to "marshal" the data, Google for CORBA and see how it marshals data -- its non-trivial.


And if you do it as strings, that's just another form of marshaling as well.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Pat Farrell wrote:
If you must do it as binary, the only reliable way is to "marshal" the data, Google for CORBA and see how it marshals data -- its non-trivial.
And if you do it as strings, that's just another form of marshaling as well.


Very true, but its a universal marshaling that is platform independent. Plus its obvious when it works, and obvious when it fails.

Many time, I think the "tools" that are claiming to "help" us are simply adding complexity and obfuscation.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're making a few wrong assumptions.

1) The size of long and int is not specified in C. An int can be 16 bits, or 32 bits, or possibly even 64 bits. A long can be 32 bits but also 64 bits.
2) You were already told about the double -> float issue.
3) A char in C is only one byte, whereas a char in Java is two. The best match would be Java's byte.
4) C has no data type for strings. A char[] is not a string. It's just a number of chars that together can be interpreted as a string, but it's still not one. A similar statement can be made about char*. The closest match to your char[6] is a byte[] of size 6.

My advice - use JNA with its data type mapping that is guaranteed to work.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:1) The size of long and int is not specified in C. An int can be 16 bits, or 32 bits, or possibly even 64 bits. A long can be 32 bits but also 64 bits.


Right. Long ago, an int was often 16 bits, because we used 16 bit computers. In the 1980s, even PCs had 32 bit processors (80386) which had, 32 bit ints.

These days, all desktop and server computers are 64 bit, so "int" is now often 64-bit. Its an "implementation detail" to the compiler, so you never can make assumptions.

All of the excitement in the C world is in embedded systems, using PIC, ARM, ATMEL, and other CPUs (often microcontrollers). They often use smaller data paths, but even there, the newer stuff uses longer words.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic