Well good luck.
Without a controlled/known output from the C++ program, it will be hard/impossible to write a java program to mimic the C++ program.
If you can have the C++ program write out a known sample of data to a file, we can write a java program to write the same bits to a file given the same sample of data.

^ like Norm said.
J

Well good luck.
Without a controlled/known output from the C++ program, it will be hard/impossible to write a java program to mimic the C++ program.
If you can have the C++ program write out a known sample of data to a file, we can write a java program to write the same bits to a file given the same sample of data.

I shouldnt have said anything about a C++ program.....

I simply want to write a array full of ints to a binary file. Nothing else. Forget about the C++ program.....

(Didnt work today as it is a holiday) I writed the writeInt method and lets see if the PLC reads it.

How many bytes and in what order do you want the bytes of the int written to the bytes of the file? Java has 4 bytes. Some systems expect ints to be low-order byte to highest and others high-order to lowest (I think java does it this way).

What is the PLC? What does it expect when it reads an int from a file?

How many bytes and in what order do you want the bytes of the int written to the bytes of the file? Java has 4 bytes. Some systems expect ints to be low-order byte to highest and others high-order to lowest (I think java does it this way).

What is the PLC? What does it expect when it reads an int from a file?

The maximum bytes are basically dinamic: Depending on the complex drawing (this is part of the C++ program but again, simply ignore that) it will be more or less. There isnt a "static" limit.

The order is the way I introduce them. If it array[0]=1 and array[1]=999 it should display

1
999

From what Ive seen (again cannot confirm this) It simply reads FIFO style from when I insert the ints into the binary file. Tommorrow at work, lets see if I can get more answers to some of your questions.

Thank you for all the help :)

I was asking how int data is stored on you computer
Take the simple case of an int with the value of 1
Here are different ways it can be stored. I show hex digits, not binary.
A hex 0 is binary 0000
0001
00000001
0100
01000000

Which one is the way it is stored on your computer?

I was asking how int data is stored on you computer
Take the simple case of an int with the value of 1
Here are different ways it can be stored. I show hex digits, not binary.
A hex 0 is binary 0000
0001
00000001
0100
01000000

Which one is the way it is stored on your computer?

My mistake. Binary numbers. The ints are all stored in 8 digit binary numbers.

1 would be
00000001

3 would be
00000011

and so on.

The ints are all stored in 8 digit binary numbers.

An eight bit value takes one byte.
The max value you can have in one unsigned byte is 255: 11111111 in binary or FF in hex

This is going nowhere. Is there someone there that understands bits and bytes and binary values when written into a file?

An eight bit value takes one byte.
The max value you can have in one unsigned byte is 255: 11111111 in binary or FF in hex

This is going nowhere. Is there someone there that understands bits and bytes and binary values when written into a file?

OK, then I would have to write 1 byte of data with those values, correct?

When I said the "maximum bytes" could be mean the array can be any size (with limitations of memory, etc)

I'm talking about ONE int value.
You have not said yet how ONE int value is stored on your computer's hard drive.
Given the layout for ONE int, a java program can write millions of them.

What program is going to read the data that the java program writes?
What does it expect the bits/bytes on the hard drive to look like if it is going to read only ONE byte?

I'm talking about ONE int value.
You have not said yet how ONE int value is stored on your computer's hard drive.
Given the layout for ONE int, a java program can write millions of them.

What program is going to read the data that the java program writes?
What does it expect the bits/bytes on the hard drive to look like if it is going to read only ONE byte?

I apoligize for simply not understand what you are asking for.

A int in my java program is written in a byte represented by 8 digits with the max being 255 as you well said because of the 8 digit limitation.

The end result that is going to read what my Java writes is a machine that makes shapes, basically.
It expects to see bytes represented as "00000001" (which is one in decimal) and other 8 digit binary numbers which equal 1 byte each.

But once again simply forget the end result: I just want to write and read arrays of ints. The PLC should read them normally just like a C++ program that does the same.

Hi riahc3
You are still missing a key point here.
"read them normally just like a C++ program" isn't an answer. There is no "normally". There are many many ways for a program to read or write binary numbers, and C++ can read all of them, depending on how the program is written. Unless you can say which of all those formats your C++ program is expecting, you can't write a file for it to read.

If, as you say, each int is written as a single byte, then you can use a DataOutputStream and its write method to write any arbitrary array of bytes. Note, however, that this cannot write the sample data that you posted earlier because that has ints >255. You cannot write arbitrary ints (in java or C++ ) to single bytes because they simply don't fit.

Hi riahc3
You are still missing a key point here.
"read them normally just like a C++ program" isn't an answer. There is no "normally". There are many many ways for a program to read or write binary numbers, and C++ can read all of them, depending on how the program is written. Unless you can say which of all those formats your C++ program is expecting, you can't write a file for it to read.

If, as you say, each int is written as a single byte, then you can use a DataOutputStream and its write method to write any arbitrary array of bytes. Note, however, that this cannot write the sample data that you posted earlier because that has ints >255. You cannot write arbitrary ints (in java or C++ ) to single bytes because they simply don't fit.

The C++ program (I really think we should use the term C++ program as it has nothing to do with it) doesnt accept anything. It just exports.

Each int is indeed written as a single byte as once again it is represented in 8 digits. I cant test it as it seems everyone here has taken a long vacation.

When I was entering data such as 88888888, these may be other numbers which havent been clarified (very little information is given to us) so Ill have to ask again.

Thanks again for all your help and I think I can give answers and more information on monday when everyone is here again.

OK, got that.
So, moving onwards, let's stop worrying about the C++ program. You say the data is read by the "PLC". What is that? Does it have any documentation about the data it expects? Do you have source code for the part where it reads the file?
ps: a small point, but it will help to keep things clear: please use the term "bits" when talking about the 8 things in a byte. Use "digits" when talking about the characters 0-9. Thanks.

OK, got that.
So, moving onwards, let's stop worrying about the C++ program. You say the data is read by the "PLC". What is that? Does it have any documentation about the data it expects? Do you have source code for the part where it reads the file?
ps: a small point, but it will help to keep things clear: please use the term "bits" when talking about the 8 things in a byte. Use "digits" when talking about the characters 0-9. Thanks.

A PLC is a programmable logic controller used in machines to give the instructions when reading certain values. The PLC (for the machine) would read in this case as a example something like 10010111 (only 1s and 0s, a binary number) in one byte and that means to stop the machine.
I have no documentation about the PLC itself except that.
Ill try to stop using the terms "bits", "bytes", "digits" as It may be confusing in a post.

Again, thanks alot for replying to the thread.

I cannot test any of this until Monday where there is someone to test this out with.

OK! That's really helpful - this is all starting to make sense now.
So we definitely need to write a sequence of single bytes.
In that case it's definitely a DataOutputStream that you need, and you can use the writeByte(int b) method for single values or the write(byte[] b, int off, int len) to write an array of bytes.
For writeByte(int b) you need to specify b as an int in the range 0-255
For write(byte[] b, int off, int len) just be careful because bytes are expressed as values -128 to +127, not 0 to 255. So you may find it easier to use writeByte in a loop.

OK! That's really helpful - this is all starting to make sense now.
So we definitely need to write a sequence of single bytes.
In that case it's definitely a DataOutputStream that you need, and you can use the writeByte(int b) method for single values or the write(byte[] b, int off, int len) to write an array of bytes.
For writeByte(int b) you need to specify b as an int in the range 0-255
For write(byte[] b, int off, int len) just be careful because bytes are expressed as values -128 to +127, not 0 to 255. So you may find it easier to use writeByte in a loop.

Well the writeByte method does look logical: It made a file that is 15 bytes in size(my array from array[0] to array[14])

On Monday Ill test it out with someone and see if thats what he wants.

More information:

They have instructed us to write out a 32 int number in binary format as a string. For example 10 would be written to the file:

00000000000000000000000000001010

written as a string.

We did a simple "if" and "else if" class that adds 0s to the left of 1010 (in case of the decimal number 10) and nothing.

We use the WriteBytes(String s) method to write it to the file but didnt work.

Any more tips?

Let me restate what you want to do.
You want to write an int to a file as a 32 character String of 0 & 1s
The output file would be a text file without any line ends. It would be one long record with the value for each int taking 32 bytes/characters.
Is that it?
To do that you would Convert the it to a String using the Integer toBinaryString() method and pad the String on the left so that it is 32 characters long. The padding can easily be done with a concatenation of leading 0s and substring taking the rightmost 32 characters

We use the WriteBytes(String s) method to write it to the file but didnt work.

Please post the code that shows the class and the method you used.
There is no WriteBytes() method. Java methods all start with lowercase letters.The could be a writeBytes() method

Let me restate what you want to do.

OK, perfect as a resume :)

You want to write an int to a file as a 32 character String of 0 & 1s

Yes. Several ints in a array. Example if in the array I have 10 then 11, the file would be this

00000000000000000000000000001010@00000000000000000000000000001010

(I put the @ to show the seperated numbers but the @ doesnt exist. They simply would be together)

The output file would be a text file without any line ends. It would be one long record with the value for each int taking 32 bytes/characters.
Is that it?

Yes, it is one long string of just 1s and 0s.

To do that you would Convert the it to a String using the Integer toBinaryString() method and pad the String on the left so that it is 32 characters long. The padding can easily be done with a concatenation of leading 0s and substring taking the rightmost 32 characters

We have a function that does that, written.

Please post the code that shows the class and the method you used.
There is no WriteBytes() method. Java methods all start with lowercase letters.The could be a writeBytes() method

My mistake, yes with writeBytes()

We have great news :)

It seems you are all indeed correct (and I was wrong of course) and the orders do matter....

(Lets see how I can explain this) If we have the number 9999 and it is written to a binary file using Java, we open the binary file in a hex editor and it comes out like this:

00 00 27 0F

now the SAME number generated but written using C++ to a binary file the number in a hex editor is shown as:

0F 27 00 00

What we plan to do is once we generate the binary file in Java, we will fill the data in a byte array and simply rewrite it but in reverse. This will give us the same output as C++.

Thanks again for all the help and any tips are still apriciated.

The java program could be made to write out the bytes in any order.

It's an int! It's a byte! It's a string! Its an int but backwards!
My head's spinning.
But here's something you may find useful, based on your most recent post:

int abcd = 9999;
int dcba = Integer.reverseBytes(abcd);

So if you use that to reverse the bytes, then write the resulting int to your file, you should get the same output as your C++ program.
NB: 1. I haven't tried this myself. 2. Java 1.5 or later.

ps. Now I have tried it. It converts 9999 = hex 00 00 27 0F to hex 0F 27 00 00

@James WOW!!! Where/how did you find that method? Late night reading?

I have no idea - somewhere in the dusty recesses of the remains of my brain there's a lorry-load of stuff that I came across somewhere sometime and thought "that's interesting". I can never remember the details, but it gives me a starting point to hunt it down again.

It's an int! It's a byte! It's a string! Its an int but backwards!
My head's spinning.

I apoligize for that. Information was not given clearly.

But here's something you may find useful, based on your most recent post:

Java Syntax (Toggle Plain Text)
int abcd = 9999;
int dcba = Integer.reverseBytes(abcd);

So if you use that to reverse the bytes, then write the resulting int to your file, you should get the same output as your C++ program.
NB: 1. I haven't tried this myself. 2. Java 1.5 or later.

ps. Now I have tried it. It converts 9999 = hex 00 00 27 0F to hex 0F 27 00 00

Intresting. I thought it was going to be harder as in load the file into a byte array and reload it but in reverse.

Will try tommorow when Im at work.

Thank you

The reverseBytes(int) method works great. Thanks.

Now I just need to do the same for doubles. I find Double.toHexString and Double.toString as good alternatives but Im not sure. We'll try them out now and see how it goes.

Thanks to everyone for their help. Will give rep points but before I gotta figure out this double thing.

Before you start messing about with those kinds of methods I would suggest you write a known double value to a file (just like you did with the int) and compare the hex with the same value written from C++. Then you will know what you need to achieve by way of swapping bytes etc.

Thanks to everyone. With your help and the libary found at http://introcs.cs.princeton.edu/java/stdlib/ I was able to try the ints and doubles.

For ints

u.write(Integer.reverseBytes(theints));

For doubles

u.write(Long.reverseBytes(Double.doubleToRawLongBits(thedoubles)));

Now Unicode files....pfff. Check for a thread in a few minutes :P

Again thanks to all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.