I a writing a program that will convert the numbers into binary form then print
ID: 3816272 • Letter: I
Question
I a writing a program that will convert the numbers into binary form then print a letter.
000 0000 0000 0000 0000 00000001 to the letter a (lowercase)
000 0000 0000 0000 0000 00000010 o the letter b (lowercase)
000 0000 0000 0000 0000 00000100 is the letter c
and if the most LHS bit is a 1, it will be 67108865 and 100 0000 0000 0000 0000 00000001 in binary wich prints A. Likewise a capital B is 100 0000 0000 0000 0000 00000010 and 6710886
I did everything right, except i'm having a problem with the uppercase letters. can someone help me print the value
A if the user enters 67108865 ? Thanks!
#include<stdio.h>
#include<string.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int to_Binary(char *argv[],int number_Of_Nums){
{
int count =0;
for (count = 0; count < number_Of_Nums; count++)
{
int deci = atoi(argv[count]);
int array[32];
int n = 0;
for(n=31; n>=0; n--)
{
if(deci>=pow(2,n)){
array[n]=1;
deci = deci-pow(2,n);
}
else
array[n] = 0;
}
//printf(" ");
//next convert binary to letter value
int temp=0;
int i,count=0;
for( n = 31; n >=0; n-- ){
// added to display the number from the first MSB As 1
if(array[n] != 1 && count==0)
{
continue;
}
else
{
count = 1;
printf( "%d", array[ n ] );
}
}
for(i=0;i<=30;i++)
{
if (array[i]==1 && array[31]==0)
temp= i+97;
if(array[i]==1 && array[31]==1)
temp= i;
}
printf("%c",(char)temp);
// this line prints the ASCII value of charcters
printf("ASCII value of %c = %d",(char)temp, (char)temp);
}
}
printf(" ");
return 0;
}
int main (int argc, char *argv[])
{
int count;
to_Binary(argv+1,argc-1);
return 0;
}
Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
int to_Binary(char *argv[],int number_Of_Nums)
{
int count =0;
for (count = 0; count < number_Of_Nums; count++)
{
int deci = atoi(argv[count]);
int array[32];
int n = 0;
for(n=26; n>=0; n--)
{
if(deci>=pow(2,n))
{
array[n]=1;
deci = deci-pow(2,n);
}
else
array[n] = 0;
}
//printf(" ");
//next convert binary to letter value
int temp=0;
int i,count=0;
for( n = 26; n >=0; n-- )
{
// added to display the number from the first MSB As 1
printf( "%d", array[ n ] ); //I have modified your code here
/*if(array[n] != 1 && count==0)
{
continue;
}
else
{
count = 1;
printf( "%d", array[ n ] );
}*/
}
for(i=0;i<=25;i++)
{
if (array[i]==1 && array[26]==0)
temp= i+97;
if(array[i]==1 && array[26]==1)
temp= i+65; //I have modified your code here
}
printf(" %c",(char)temp);
// this line prints the ASCII value of charcters
printf(" ASCII value of %c = %d",(char)temp, (char)temp);
printf(" ");
}
return 0;
}
int main (int argc, char *argv[])
{
int count;
to_Binary(argv+1,argc-1);
return 0;
}
input:-
1 2 4 8 16 32 64 67108865 67108866 67108868 67108872 100663296
output:-
000000000000000000000000001 a ASCII value of a = 97
000000000000000000000000010 b ASCII value of b = 98
000000000000000000000000100 c ASCII value of c = 99
000000000000000000000001000 d ASCII value of d = 100
000000000000000000000010000 e ASCII value of e = 101
000000000000000000000100000 f ASCII value of f = 102
000000000000000000001000000 g ASCII value of g = 103
100000000000000000000000001 A ASCII value of A = 65
100000000000000000000000010 B ASCII value of B = 66
100000000000000000000000100 C ASCII value of C = 67
100000000000000000000001000 D ASCII value of D = 68
110000000000000000000000000 Z ASCII value of Z = 90
Process exited normally.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.