c - 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. 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. It should produce a vertical A but instead produces a horizontal 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 :
01111110
00010001
00010001
00010001
01111110
must produce a vertical A turning left (missing 0 not present here):
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
推荐阅读
- javascript - 当我在输入中输入一个数字时,第一个函数开始
- python - 在 django 模型中正确使用 limit_choices_to
- c# - 从选择列表中获取空值(下拉列表)
- javascript - ajax调用后Javascript中断
- python - 如何使用 X-Ray 和 SNS 获得一个大链而不是多个小链?
- php - SwiftMailer 测试用户名和密码
- database - 哪个数据库最适合跟踪用户在网站上的活动以了解用户兴趣
- python - python - 加载模型时如何使用Tensorflow / Keras修复'AttributeError:'list'对象在python中没有属性'shape''错误
- amazon-web-services - CloudFormation:无法使用无服务框架在 AWS 上为 SNS 主题创建策略
- python-3.x - 如何修复python中的“AttributeError:模块'pycocotools'没有属性'mask'”错误