0

だからここに私がしなければならない/しなければならないことがあります。132x72 の大きな画像を含む .txt ファイルがあります。私がする必要があるのは、それを16進値のAC配列に入れることです。

上位 8 行の最初の文字を取得し、それらを水平方向にまとめて、16 進数に変換できるようにする方法を見つける必要があります。次に、それを9回行う必要があります。

例:

00000
00000
11111
01010
10101
10101
01010
10101

私が変わる必要があること:

00101101
00110010
00101101
00110010
00101101

これを行うための最良/最も簡単な方法は何ですか? 正直なところ、どこから始めればよいかわかりません。

4

2 に答える 2

2

.txt ファイル内の 1と 0 が文字であると仮定すると(バイナリの場合は、最初に変換する必要があります)、ファイルを行ごとに配列に読み込むだけです。次に、ストライドで配列を印刷できます。つまり、最初に文字 0、8、16、24 ... を印刷し、次に 1、9、17、... などと印刷します。

for (i = 0; i < ROWS; i++) {
    for (j = 0; j < COLS; j++) {
        printf("%c", chars[i + j * ROWS]);
    }
    printf("\n");
}

そんな感じ。

于 2012-04-19T17:38:39.983 に答える
0

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)

于 2012-04-19T17:58:02.847 に答える