In: Computer Science
How could this be implemented in C?
binstring[] = 1011001000010110000100010100110010010101101100011101000110010000100010100110010010101101100011101101000010001010011001001010110110001110001010011001001111100001000100010001000100010001
~ 160 characters long divided into groups of 5 ~
This function takes a starting position and the array of ‘1’ and ‘0’ and returns the character
representation of the five bits at that location.
char byte_at( int position, const char binstring[] ) {
}
file name 1.c
#include<stdio.h>
#include<math.h>
char byte_at( int position, char *binstring ) {
int i=0,j=4;
int tmp=0;
while(i<5)
{
tmp+=(binstring[position+j]-48)*pow(2,i);
//starting from 5th bit from the position calculate the decimal representation of 5 bit binary .
//'0' is 48 in decimal and '1' is 49
i++;
j--;
}
return tmp;
}
int main()
{
int position=0;
char binstring[] = "1011001000010110000100010100110010010101101100011101000110010000100010100110010010101101100011101101000010001010011001001010110110001110001010011001001111100001000100010001000100010001";
do
{
scanf("%d",&position);
}while( position > 160); //loop to check value enter is between the range i.e less than 160
printf("character at the position %d are: %c " ,position ,byte_at( position, binstring ) );
}
compile the program
gcc -o 1 1.c -lm