简体   繁体   English

如何使水平矩阵垂直?

[英]How to turn a horizontal matrix vertical?

I'm converting specific hexadecimals based on the entered character to decimals and then to binary. 我正在根据输入的字符将特定的十六进制转换为十进制,然后转换为二进制。 I'm trying to print a matrix of the 1s and 0s in the shape of the entered character. 我正在尝试以输入字符的形状打印1和0的矩阵。 When I print the first character it outputs sideways. 当我打印第一个字符时,它会横向输出。 How can I turn it vertical? 如何将其垂直放置?

I tried finding a way to print just the first digit of each binary on one row of columns, the second digit on the 2nd row of columns, and so on, but came up with nothing. 我试图找到一种方法,仅在行的一列上打印每个二进制文件的第一位,在列的第二行上打印第二位,依此类推,但是没有任何结果。 The code requires the use of an external file with all of the hexadecimals. 该代码要求使用带有所有十六进制的外部文件。 When pulled from the file, the hexadecimals for A are in decimal form: 126, 17, 17, 17, 126. I then convert them to a binary integer: 01111110, 00010001, 00010001, 00010001, and 01111110. And print them out in a 5x7 matrix. 从文件中提取时,A的十六进制为十进制形式:126、17、17、17、126。然后将它们转换为二进制整数:01111110、00010001、00010001、00010001和01111110。 5x7矩阵。 It should produce a vertical A but instead produces a horizontal A. 它应该产生一个垂直的A,但是会产生一个水平的A。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "font5x7.h"

void DecToBin (int newBin[], char userMessage[], int k) {
int i = 0;
int j = 0;
int m = 0;

for (i = 0; i < 1; ++i) {
    while (m < 8) {
    newBin[j] = Font5x7[k] % 2;
    Font5x7[k] = Font5x7[k] / 2;
    ++j;
    ++m;
    }
}
}

int main (void) {

const int INPUT_STR_SIZE = 40;
char userMessage[INPUT_STR_SIZE];
int i = 0;
int j = 0;
int k = 0;

const int BIN_MAX = 8;
int binNum[BIN_MAX];

printf("Enter message:\n");
fgets(userMessage, INPUT_STR_SIZE, stdin);



//Iterate through DecToBin function for bin equivalents
for (i = 0; i < strlen(userMessage); ++i) {
    if (userMessage[i] == 'A') {
        for (k = 165; k < 170; ++k) {
            DecToBin(binNum, userMessage, k);
                for (j = 7; j >= 0; --j) {
                    printf("%d", binNum[j]);
                }
                    printf("\n");
        }
    }
}






return 0;

} }

If I well understand the horizontal A with pixels from 126, 17, 17, 17, 126 : 如果我很好地理解了像素126、17、17、17、126中的水平A:

01111110
00010001
00010001
00010001
01111110

must produce a vertical A turning left (missing 0 not present here): 必须产生一个垂直的A向左转(此处未缺少0):

01110
10001
10001
10001
11111
10001
10001
00000

That means : (the upper digit is the index in the array, the lower the bit rank) 这意味着:(高位是数组中的索引,低位是位)

07 06 05 04 03 02 01 00
17 16 15 14 13 12 11 10
27 26 25 24 23 22 21 20
...
n7 n6 n5 n4 n3 n2 n1 n0

have to produce 必须生产

00 10 20 30 .. n0
01 11 21 31 .. n1
02 12 22 32 .. n2
...
07 17 27 37 .. n7

A proposal to do that : 这样做的建议:

#include <stdio.h>
#include <string.h>

void turnLeft(const unsigned char * v, unsigned char r[8], size_t sz)
{
  memset(r, 0, sizeof(r));

  size_t j = 0;

  for (unsigned m = 1; m != (1 << 8); m <<= 1) {
    for (size_t i = 0; (i != sz) && (i != 8); ++i) {
      if (v[i] & m)
        r[j] |= (1 << (7 - i));
    }
    j += 1;
  }
}

void pr(const unsigned char * v, size_t sz)
{
  for (size_t i = 0; i != sz; ++i) {
    unsigned char c = v[i];

    for (unsigned m = (1 << 7); m != 0; m >>= 1)
      putchar('0' + ((c & m) != 0));

    putchar('\n');
  }
}

int main()
{
  const unsigned char v[] = { 126, 17, 17, 17, 126 };
  unsigned char r[8];

  pr(v, sizeof(v));
  putchar('\n');

  turnLeft(v, r, sizeof(v));
  pr(r, 8);
}

Compilation and execution : 编译执行:

vxl15036 /tmp % gcc -std=c99 -pedantic -Wextra a.c
vxl15036 /tmp % ./a.out
01111110
00010001
00010001
00010001
01111110

01110000
10001000
10001000
10001000
11111000
10001000
10001000
00000000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM