create a program that converts decimal numbers -from 0 to 255 -, which the user types, to binaries. I can already convert normally and show the result reversed, which does not solve. I then thought of assigning each "do while" loop a position in a vector (banana) using the for, so that I can then invert it using strrev(). However, I could not. Some of you could give me a light to solve this problem?
Wouldn’t be complicating things too much? It’s a byte. You don’t have to reverse anything. Positional notation already solved this in the Hindu system in the first centuries d.c, the same system used until today.
about your program
int nusr, resto;
long int banana[len];
scanf("%i", &nusr);
if (0<=nusr && nusr<=255){
- always test the scanf() return. What is the purpose of following if not reading anything? What will be the value of
nusr
?
- if you are going to read something between 0 and 255 inclusive, because you are reading in a
int
and then test what you read? Wouldn’t it be better to read a unsigned char
After all? What has a value between 0 and 255 inclusive and occupies... one byte in the popular eight bits? This reading in a value that in general has up to 4 bytes and a signal, to later test if it is positive and if it fits in one byte only. Let the compiler work for you:
[![Direct from manual][1]][1]
This means you just need to use the specifier "%hhu"
when calling scanf()
2 Examples in C
Converting a byte to binary
The statement is vague if you have one. So let’s imagine the output in the format
nnn em binario = 12345678
As in
128 em binario = 10000000
1 em binario = 00000001
0 em binario = 00000000
255 em binario = 11111111
Each bit of the string is worth, of course, 0 or 1. It turns out they are the representation of the entered value. In decimal, 123 is 1 x 100 + 2 X 10 + 3...
What has each position, in binary? The bits are worth the power of 2 corresponding, 1,2,4,8,16,32,64 and 128, ie
bit i = 1 << i
Moreover:
- in C an expression is 0 if false and 1 if true
- the value of
'1'
is 1 + '0'
And so you don’t need anything else to convert byte to binary into a hypothetical string of 0
and 1
declared
char binario[] = "12345678"; // 8 bits
And
binario[7] = '0' + ((N & 1) != 0)
binario[6] = '0' + ((N & 2) != 0)
binario[5] = '0' + ((N & 4) != 0)
binario[4] = '0' + ((N & 8) != 0)
binario[3] = '0' + ((N & 16) != 0)
binario[2] = '0' + ((N & 32) != 0)
binario[2] = '0' + ((N & 64) != 0)
binario[0] = '0' + ((N & 128) != 0)
Example 1:
#include <stdio.h>
int main(void)
{
unsigned char N = 0;
char binario[] = "12345678"; // 8 bits
printf("Valor a converter para binario (entre 0 e 255 incl.) : ");
int res = scanf("%hhu", &N); // o simples: byte sem sinal
if ( res != 1 ) return -1;
binario[7] = '0' + ((N & 1) != 0); // o bit 0 fica do lado direito
binario[6] = '0' + ((N & 2) != 0); // porque e uma string
binario[5] = '0' + ((N & 4) != 0);
binario[4] = '0' + ((N & 8) != 0);
binario[3] = '0' + ((N & 16) != 0);
binario[2] = '0' + ((N & 32) != 0);
binario[2] = '0' + ((N & 64) != 0);
binario[0] = '0' + ((N & 128) != 0); // esse e o bit 7
printf("\n\t%3d em binario = %s\n", N, binario);
return 0;
}
No loops, no functions except reading.
Another example, with loop
#include <stdio.h>
int main(void)
{
unsigned char N = 0;
char binario[] = "12345678"; // 8 bits
printf("Valor a converter para binario (entre 0 e 255 incl.) : ");
int res = scanf("%hhu", &N); // o simples: byte sem sinal
if ( res != 1 ) return -1;
for ( int i = 0; i<8; i+=1)
binario[7-i] = '0' + ((N & (1<<i)) != 0);
printf("\n\t%3d em binario = %s\n", N, binario);
return 0;
}
A little shorter.
[1]: https://i.stack.imgur.com/prNBB.png
The lines
(float)nusr;
andnusr / 2;
are not assigning the value to any variable, so can be removed. Division of twoint
'is always anotherint
then onwhile
don’t need to cast, it may just bewhile (nusr != 0);
. If you have the samereturn 0
within theif
and ofelse
, then you could take them off and just have thereturn 0
at the end. Noelse
, theif(nusr)
can be removed. And vc can store the digits in the arraybanana
and when printing, run it backwards (for (int i = len - 1; i >= 0; i--) imprime banana[i]
- but change the name to something better, likedigitos
) :-)– hkotsubo
Thank you so much for the tips! Just one more thing: I put the for for out of the "do while", so the loop stay correct, but I don’t know how to write. I tried digits[i]=rest; but I cannot assign values, it appears (null)...
– Vitor L.
You have to [Dit] the question and put the code you tried, otherwise you have no way of knowing where the error is. Anyway, a suggestion: https://ideone.com/8Ov5tT
– hkotsubo