Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Again, I/O between C/C++ and Java

Reply
Thread Tools

Again, I/O between C/C++ and Java

 
 
RC
Guest
Posts: n/a
 
      08-30-2007
This is continuation for my August 24th posted

"How to convert int/float etc into byte[]?"

float f = -10.2;

Here is a C/C++

// write 1 byte of float to the file pointer
fwrite((char *)f, sizeof(float), 1, fp);

In Java, if I used DataOutputStream class

dataOutputStream.writeFloat(f);

But when I compared the content of two
binary files (C/C++ and Java) by command

"od -a binFile"
or
"od -c binFile"


The output binary file written by Java is
different from the one written by C/C++

Then I tried different way:

byte[] byteArrays = (Float.toString(f)).getBytes();
dataOutputStream.write(byteArrays, 0, byteArray.length);


This time the binary becomes ASCII as "-10.2"

Then, I tried one more way:

byte[] byteArray = new byte[1];
byte b = (Float.valueOf(f)).byteValue();
byteArray[0] = b;
dataOutputStream.write(byteArrays, 0, byteArray.length);


But still no luck, the output binary file in Java
is still not the same as one in C/C++.

So back to my original question, what is the correct
way to convert a float, int, double, short, long, etc.
into a byte array?

Thank Q very much!
 
Reply With Quote
 
 
 
 
Hunter Gratzner
Guest
Posts: n/a
 
      08-30-2007
On Aug 30, 8:54 pm, RC <(E-Mail Removed)> wrote:
> So back to my original question, what is the correct
> way to convert a float, int, double, short, long, etc.
> into a byte array?


The correct way is to first specify the format you want to have in the
file in an non-ambiguous way. Then you implement the specification in
both languages.

Just because C can be used to dump the contents of a memory region 1:1
to a file doesn't mean you have a format specification of any kind.

 
Reply With Quote
 
 
 
 
RC
Guest
Posts: n/a
 
      08-30-2007
Hunter Gratzner wrote:
> On Aug 30, 8:54 pm, RC <(E-Mail Removed)> wrote:
>> So back to my original question, what is the correct
>> way to convert a float, int, double, short, long, etc.
>> into a byte array?

>
> The correct way is to first specify the format you want to have in the
> file in an non-ambiguous way. Then you implement the specification in
> both languages.
>


I agree with you. I prefer use format fprintf(fp, "%5.1f", f); in C/C++

But that C program was written by other programmer, he/she used
fwrite function without format. That binary file written by
C is read by another C program.

Now I have to write in Java produce the same binary file for that
program to read. That binary file should be able read by Java, C/C++
program.

> Just because C can be used to dump the contents of a memory region 1:1
> to a file doesn't mean you have a format specification of any kind.
>

 
Reply With Quote
 
Steve Wampler
Guest
Posts: n/a
 
      08-30-2007
RC wrote:
> But that C program was written by other programmer, he/she used
> fwrite function without format. That binary file written by
> C is read by another C program.
>
> Now I have to write in Java produce the same binary file for that
> program to read. That binary file should be able read by Java, C/C++
> program.


Have you looked at the bit/byte patterns using "od -tx1" to see if the
difference is something obvious? Like big-vs-little endian?
(C/C++ will dump the value in whatever the native order is
while I would expect [without testing, mind you!] Java to use
a standard representation regardless of the native byte ordering.
Try dumping a 4-byte integer both ways and compare to see if
the byte-ordering is an issue.

--
Steve Wampler -- http://www.velocityreviews.com/forums/(E-Mail Removed)
The gods that smiled on your birth are now laughing out loud.
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      08-30-2007
RC wrote:
> Hunter Gratzner wrote:
>> On Aug 30, 8:54 pm, RC <(E-Mail Removed)> wrote:
>>> So back to my original question, what is the correct
>>> way to convert a float, int, double, short, long, etc.
>>> into a byte array?

>>
>> The correct way is to first specify the format you want to have in the
>> file in an non-ambiguous way. Then you implement the specification in
>> both languages.
>>

>
> I agree with you. I prefer use format fprintf(fp, "%5.1f", f); in C/C++


A file can have a known, defined format without being a text file. A
format specification can say things like "32 bit binary integer, 2's
complement, little endian byte order.".

> But that C program was written by other programmer, he/she used
> fwrite function without format. That binary file written by
> C is read by another C program.
>
> Now I have to write in Java produce the same binary file for that
> program to read. That binary file should be able read by Java, C/C++
> program.


You still need to beg, borrow, steal or create a file format
specification. If you know the system and implementation of C++ on which
the program was created, as well as the C struct definition, you should
be able to find out the format.

If you have access to any C++ programmer who is familiar with the system
and C++ implementation they should be able to tell you the mapping from
C++ struct to file format.

I suspect that if you posted the information about the C++ environment
and the struct, someone here could give you some tips. As it is, I have
no idea of some of the most basic information about your file.

Patricia
 
Reply With Quote
 
a24900@googlemail.com
Guest
Posts: n/a
 
      08-30-2007
On Aug 30, 9:22 pm, RC <(E-Mail Removed)> wrote:
> But that C program was written by other programmer, he/she used
> fwrite function without format. That binary file written by
> C is read by another C program.


--->%--->%--->%--->%--->%--->%--->%--->%--->%--->%--->%--->%--->%--->
%---
From: "Paul Lutus" <(E-Mail Removed)>
Newsgroups: gnu.gcc.help,gnu.g+
+.help,comp.unix.programmer,comp.unix.solaris,comp .lang.c )

Subject: Re: HELP: struct data members alignment???
Date: 07 Jun 1999 00:00:00 GMT

About this subject, and in seemingly endless threads, we get two kinds
of
posts:

1. Why can't I just write raw structure data to a file or network
socket?
Writing it as text or packaging it in a portable, binary form is a lot
of
work, and I probably won't be moving the program to a different
platform
anyway.

2. How come my program on platform B cannot read the raw structure
data from
platform A?

The answer to (1) is (2). The answer to (2) is (1).

--

Paul Lutus
www.arachnoid.com
--->%--->%--->%--->%--->%--->%--->%--->%--->%--->%--->%--->%--->%--->
%---

Source: http://www.rdrop.com/~cary/html/endian_faq.html

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      08-30-2007
RC wrote:
>>>> So back to my original question, what is the correct
>>>> way to convert a float, int, double, short, long, etc.
>>>> into a byte array?


Hunter Gratzner wrote:
>>> The correct way is to first specify the format you want to have in the
>>> file in an non-ambiguous way. Then you implement the specification in
>>> both languages.


Patricia Shanahan wrote:
> You still need to beg, borrow, steal or create a file format
> specification. If you know the system and implementation of C++ on which
> the program was created, as well as the C struct definition, you should
> be able to find out the format.


By my count, this is at least four times you've received this same answer, RC.

It was explained that Java emits big-endian values, and C++ on many platforms
(not all) emits little-endian. It was explained that C++ binary file formats
vary according to the compiler options and other aspects of the C++
environment. It was explained that you need to know which format applies in
your specific case in order to get Java to emit the same data. Converting
float to byte doesn't do it, because Java performs a value conversion rather
than a bitwise mapping to do that. Simply writing arbitrary file formats
doesn't work, as you've discovered, because you are not controlling your IO to
match what the C++ program does. Indeed, you have yet to analyze what the C++
program does, an essential first step.

So instead of asking the question again to get the same answer, ascertain what
the C++ file format is. Then use one of the tricks provided to you by people
here to get Java to write the exact same format.

--
Lew
 
Reply With Quote
 
Steve Wampler
Guest
Posts: n/a
 
      08-30-2007
Patricia Shanahan wrote:
> RC wrote:
>> Hunter Gratzner wrote:
>>> On Aug 30, 8:54 pm, RC <(E-Mail Removed)> wrote:
>>>> So back to my original question, what is the correct
>>>> way to convert a float, int, double, short, long, etc.
>>>> into a byte array?

....
> A file can have a known, defined format without being a text file. A
> format specification can say things like "32 bit binary integer, 2's
> complement, little endian byte order.".


Nicely put. To expand just a bit...

In RC's case, it has a known defined order that will vary depending
upon the architecture the C program is run on. The C code he gave
for writing the float is very specific to the native architecture.
Changing that architecture (by going from x86 to SPARC, say) will
change the output. So it's probably safe to assume that Patricia
is spot on with the caveat that the format can probably be determined
by simple inspection using 'od'.

Since the Java program is constrained to produce the exact same
format that is currently produced, it suffices to determine that
native order and cobble some code to force that order. One
particularly ugly approach would be to use Float.floatToIntBits(f)
and then use shifting and masking to extract the bytes for
further processing.

The nice thing about the Java version versus the (original) C/C++
version is that the Java solution will then produce that format
independent of the native architecture.

--
Steve Wampler -- (E-Mail Removed)
The gods that smiled on your birth are now laughing out loud.
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      08-30-2007
Steve Wampler wrote:
> Patricia Shanahan wrote:
>> RC wrote:
>>> Hunter Gratzner wrote:
>>>> On Aug 30, 8:54 pm, RC <(E-Mail Removed)> wrote:
>>>>> So back to my original question, what is the correct
>>>>> way to convert a float, int, double, short, long, etc.
>>>>> into a byte array?

> ...
>> A file can have a known, defined format without being a text file. A
>> format specification can say things like "32 bit binary integer, 2's
>> complement, little endian byte order.".

>
> Nicely put. To expand just a bit...
>
> In RC's case, it has a known defined order that will vary depending
> upon the architecture the C program is run on. The C code he gave
> for writing the float is very specific to the native architecture.
> Changing that architecture (by going from x86 to SPARC, say) will
> change the output. So it's probably safe to assume that Patricia
> is spot on with the caveat that the format can probably be determined
> by simple inspection using 'od'.


Yes, even if I think I know how a particular compiler lays things out, I
would at least want to check against an 'od' to see if reality matches
my theory.

>
> Since the Java program is constrained to produce the exact same
> format that is currently produced, it suffices to determine that
> native order and cobble some code to force that order. One
> particularly ugly approach would be to use Float.floatToIntBits(f)
> and then use shifting and masking to extract the bytes for
> further processing.
>
> The nice thing about the Java version versus the (original) C/C++
> version is that the Java solution will then produce that format
> independent of the native architecture.
>


I'm not at all sure it will be necessary to do any bit twiddling at all.
If the file is big endian, DataOutputStream should do the job after
working out a few details of e.g. whether there is padding and if so
where, and which Java type to use for each field. If the file is little
endian, then Roedy's LEDataOutputStream does the job.

Patricia
 
Reply With Quote
 
~kurt
Guest
Posts: n/a
 
      08-31-2007
RC <(E-Mail Removed)> wrote:
>
> But that C program was written by other programmer, he/she used
> fwrite function without format. That binary file written by
> C is read by another C program.


What platform is the program that creates the file running on?

- Kurt
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 01-06-2011 05:00 PM
RMI between Java 1.4 and Java 1.5 problem Fran Cottone Java 1 03-22-2005 11:47 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Differnce between Java Bean and Enterprise Java Bean Markku Salminen Java 3 01-21-2004 09:25 AM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments