sry it's not System.println();, it's System.out.println();
for(int i = 0; i < array.length; i++){
System.out.print("[" + i + "]: " + array[i]);
}
Main Topics
Browse All TopicsI have a file that countains a double as written by a c program on intel. I need to read this file from java
in c I can read the file as:
char *ba;
double d;
memcpy(&d, &ba[offset], 8);
in java how do I do it?
For a test the bytes are:
[0]: 0 [1]: 0 [2]: 0 [3]: 0 [4]: 64 [5]: 248 [6]: 216 [7]: 64 = 25569.000000
as printed from:
printf("[0]: %d [1]: %d [2]: %d [3]: %d [4]: %d [5]: %d [6]: %d [7]: %d = %f\n" ,
ba[offset],ba[offset+1],ba
ba[offset+4],ba[offset+5],
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
maheshexp,
Your totally confused, I want to copy a byte array into a double
ie
byte ba[] = new byte[]{0,0,0,0,64,248,216,
double = convert(ba);
and then I want double to equal: 25569.000000
keeping in mind that the byte array above came form a memory dump of eight bytes running on an intel processoer
orangehead911,
once again i'm dealing with a byte array. Doesn't anybody ever read the damn question?????
but anyway
java.io.ByteArrayInputStre
java.io.DataInputStream in = new java.io.DataInputStream(ba
returns: 5.385563126E-315 which is != 25569.000000
calvinrsmith, I understand your problem, I have the same, even with integers (INTEL <-> MOTOROLA <-> VAX ...).
If you can avoid it, never exchange binary formats between different languages.
THIS IS THE STRENGTH OF JAVA: doubles are written (and read) as platform independent
and even CPU accelerator independent format.
The only thing you can do now is find out what the difference is between the source format (probably not IEEE)
and the JAVA standard. Hard work: where's the exponent, its sign, the mantissa, that's sign .....
The best way to exchange such data is in ASCII format.
And don't get angry that people overlook your question details, I've had even worse ........
;JOOP!
>> this has nothing to do with reading/writing binaries within a JAVA situation.
True
>> So it has no use reading that byte array as a byte-file,
True
>> it was not created in JAVA, so it won't work!
Not true, it has to do with the way the primitive type is serialized to disk. If this wasn't possible in Java, Java would suddenly not be as useful as it is.
Your recommendation to only write out into an easily exchangeable format is very valid, however not applicable to all situations, e.g. data files output from legacy applications.
I got it working,
i had to modify the bytes and then or them in then finally tell java to make a double out of them
for anyone else that finds this question and wants to know how to do it visit:
http://mindprod.com/jgloss
My code looks like this:
byte ba[] = new byte[]{0,0,0,0,64,248,216,
int currentByte = 0;
long accum = 0;
for ( int shiftBy = 0; shiftBy < 64; shiftBy +=8 )
{
// must cast to long or shift done modulo 32
accum |= ( (long)(mdb.pg_buf[offset+c
}
d = Double.longBitsToDouble (accum);
and d will be the correct answer: 25569.0
Business Accounts
Answer for Membership
by: maheshexpPosted on 2004-05-05 at 20:35:14ID: 11002068
public static void arraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length)
Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest. The number of components copied is equal to the length argument. The components at positions srcPos through srcPos+length-1 in the source array are copied into positions destPos through destPos+length-1, respectively, of the destination array.
If the src and dest arguments refer to the same array object, then the copying is performed as if the components at positions srcPos through srcPos+length-1 were first copied to a temporary array with length components and then the contents of the temporary array were copied into positions destPos through destPos+length-1 of the destination array.
to print user System.println();