This is an interesting format. In any event, read in a line and then add values appropriately to an array. This is what I mean:
Input Line 1: 01101
would correspond to some array: image[0][0] = 0, image[1][0] = 1 ...
This may be best done with std::vector
using the push_back()
method.
// If you know the image size already
unsigned char image[NUM_ROWS][NUM_COLS/8]; // 8 bits per byte
std::ifstream file("yourfile.txt", std::ifstream::in);
// Initialize the array to 0 with memset or similar
// Read the whole file
int rows = 0;
int cols = 0;
while(!file.eof) {
std::string line;
// Get line by line
std::getline(file, line);
// Parse each line (probably better in another function)
const char* str = line.c_str();
while(str[rows] != '\0') {
unsigned val = str[rows] - '0'; // Convert to int
unsigned shift = 8 - (rows % 8); // 8 bits per byte - this is tricky big-endian or little endian?
image[rows][cols/8] |= val << shift; // Convert to int val and pack it to proper position
rows++;
}
cols++;
rows = 0;
}
file.close();
The code is untested, but should give you a rough idea on how to read the data in properly. Now you have a properly formatted 2-dimensional array with your values (this is what the shifting was for). From here, you can take these values as int
values and convert them appropriately (a base 16 conversion is trivial from binary - i.e. each byte has two hexadecimal digits)