How to copy 2 bytes from a file an put into result variable?

In summary, you first need to open the file in binary mode, and then use the bitwise and operator to extract the low byte and high byte. You can then use these values to calculate the number.
  • #1
Leannie
7
0
Hi! I don't know if I am even wording this right but here goes. I have to write a programme to read two data files into C++, then ouput as one file in the same format.

The two files are files of 10 bit data. Each 10-bit recorded value is represented by a 16 bit word, of which the top 6 bits are not in use.

I have managed to read in the files using "ifstream inFile(. . . . .)"

I want to ouput the result as ((data1-data2)/(data1+data2))*1000.

I have tried using "inFile.get()" but that only takes 1 byte from the file and puts it into my result variable. Hence I end up with every value as 00. I want to take the first byte and times by 256 (100 in hexidecimal) and add the 2nd byte on.

My problem is how do I copy 2 bytes of data?

I know it is something to do with byte swapping, lobyte and high byte and I want C=A*256+B where every A is 00. But I have no idea how to do this?

Any help would be great,
thanks in advance :)
 
Physics news on Phys.org
  • #2
When you use the ifstream constructor, in what mode are you opening the file? It seems to me you should be opening it in binary mode, not in text mode.
 
  • #3
Hi! I am opening it in binary format as below, I do the same with both files so do this twice.

ifstream inFile1("h:\\data1.dat",ios::in,ios::binary);

I don't know how write code to multiply the first byte by 256 and add on the second byte though. I want to place this new value in my result variable so that I can output my results as required.

Thanks
 
  • #4
You need to read two bytes from the file, and then separate these into high and low bytes.

Let's suppose you have read a value into an unsigned short int varible named input_data. This data type should be two bytes or 16 bits, but you should check that.
To get the low byte and high byte, use the bitwise "and" operator, &, with suitable bit masks.
lo_byte = input_data & 0xFF;
hi_byte = input_data & 0xFF00;

The first statement above puts the low 8 bits into lo_byte. The second statement puts the high 8 bits into hi_byte.

To get back the number, do this:
number = 256 * hi_byte + lo_byte;
 
  • #5
Thanks very much! I will try that as soon as I go into uni tomorrow :D Glad you knew what I meant, I'm rubbish at wording things. Thanks again, think that's exactly what I'm looking for :)
 

1. How do I copy 2 bytes from a file?

To copy 2 bytes from a file, you will need to use a file input stream to read the file and a byte array to store the copied bytes. Use the read(byte[] b, int off, int len) method to read 2 bytes from the file and store them in the array.

2. Can I copy more or less than 2 bytes from a file?

Yes, you can copy any number of bytes from a file by changing the len parameter in the read method. However, make sure to adjust the size of the byte array accordingly.

3. How do I put the copied bytes into a result variable?

To store the copied bytes in a result variable, you can use the System.arraycopy method. This method will copy the bytes from the byte array into the result variable.

4. What if the file does not have 2 bytes to copy?

If the file does not have 2 bytes to copy, the read method will return the number of bytes that were successfully read. You can then check this value and handle the situation accordingly.

5. Can I copy bytes from a specific location in the file?

Yes, you can specify the starting position of the bytes you want to copy by using the off parameter in the read method. Keep in mind that the first byte in a file has an offset of 0.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
548
  • Engineering and Comp Sci Homework Help
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
735
  • Introductory Physics Homework Help
Replies
1
Views
925
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
16
Views
3K
Back
Top