Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes c to java data type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "c to java data type" Watch "c to java data type" New topic
Author

c to java data type

bruce truong
Greenhorn

Joined: Mar 14, 2011
Posts: 11
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

Joined: Apr 06, 2010
Posts: 4343
    
    8

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
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 21, 2008
Posts: 1107

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
bruce truong
Greenhorn

Joined: Mar 14, 2011
Posts: 11
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
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

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

Joined: Mar 14, 2011
Posts: 11
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

Joined: Aug 11, 2007
Posts: 4646
    
    5

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Aug 11, 2007
Posts: 4646
    
    5

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

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: c to java data type
 
Similar Threads
Is This a Widening or an Upcast?
Legal Data Types
Implicit Conversion?
char z =1; no single quotes OK?
Equality of primitives