Question: RGB You are given a binary file that holds ( mathbf { 1 6 } ) different RGB values. Your job is

RGB
You are given a binary file that holds \(\mathbf{16}\) different RGB values. Your job is to read the binary files, stored the RGB values appropriately in the given struct RGB\{\}, and store all the RGB objects in a
. Once this is done, iterate through the vector and print out each individual RGB value for the RGB Value \#.
You are given a helper function toHex(unsigned char value), you will implement this like
```
RGB pixel(0,255,255);
cout toHex(color.r)""; //prints 00
cout toHex(color.g)""; //prints FF
cout toHex(color.b)""; //prints FF
```
Reading a Binary File
1. Open the file in binary mode
2. Use function to read raw bytes
3. Specify the number of bytes to read
4. Store the data in an appropriate buffer (e.g., array,
5. Process the binary data as needed ```
#include
#include
#include
#include
using namespace std;
struct RGB {
unsigned char R, G, B;
RGB() : R(0), G(0), B(0){}//default constructor
RGB(unsigned char r, unsigned char g, unsigned char b) : R(r), G(g), B(b){}//paramaterized constructor
//if you're confused on the constructor syntax, you will learn this when class inheritance is brought up.
};
string toHex(unsigned char value){
const char* hex_chars ="0123456789ABCDEF"; //create hex values using char*
string result;
result += hex_chars[(value >>4) & 0xF]; //bitshift left 4 bytes & logical AND (gets first hex digit)
result += hex_chars[value & 0xF]; //logical AND (gets second hex digit)
return result; //returns the hex value of a given unsigned char as a string
}
int main(){
const string filename = "student/binary/colors.bin";
ifstream file(filename, ios::binary);
// Read RGB values from binary file
vector colors;
//open file in binary
if (!file){
cerr "Error opening file for reading: " filename endl;
return 1;
}
RGB obj(unsigned char r, unsigned char g, unsigned char b);
//create an RGB object, read all hex values in binary file
//hint, use a while loop
``````
//hint, use a while loop
file.close();
// Print the RGB values
for (int i =0; i colors.size(); ++i){
RGB color = colors[i];
cout "RGB Value #"(i +1) endl;
//use toHex to print the RGB values
RGB pixel(0,255,255);
cout toHex(color.r)""; //prints 00
cout toHex(color.g)""; //prints FF
cout toHex(color.b)""; //prints FF
cout endl;
}
return 0;
}
```
RGB You are given a binary file that holds \ ( \

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!