Medium
You are taking part in an Escape Room challenge designed specifically for programmers. In your efforts to find a clue, you've found a binary code written on the wall behind a vase, and realized that it must be an encrypted message. After some thought, your first guess is that each consecutive 8 bits of the code stand for the character with the corresponding extended ASCII code.
Assuming that your hunch is correct, decode the message.
Example
For code = "010010000110010101101100011011000110111100100001", the output should be
messageFromBinaryCode(code) = "Hello!".
The first 8 characters of the code are 01001000, which is 72 in the binary numeral system. 72 stands for H in the ASCII-table, so the first letter is H.
Other letters can be obtained in the same manner.
Input/Output
-
[execution time limit] 0.5 seconds (c)
-
[input] string code
A string, the encrypted message consisting of characters '0' and '1'.
Guaranteed constraints:
0 < code.length < 800. -
[output] string
- The decrypted message.
// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
char * answer = malloc(strlen(name) + 8);
printf("This prints to the console when you Run Tests");
strcpy(answer, "Hello, ");
strcat(answer, name);
return answer;
}
Solution
char *messageFromBinaryCode(char *code)
{
char *decimalCode=(char *)calloc(strlen(code)/8+1,sizeof(char));
for(int i=0;i<strlen(code);i+=8)
{
int N=0;
for(int j=0;j<8;j++)
N+=pow(2,7-j)*(code[i+j]=='1');
decimalCode[i/8]=N;
}
return decimalCode;
}
'Codesignal' 카테고리의 다른 글
<Codesignal> Sudoku (0) | 2020.04.06 |
---|---|
<Codesignal> spiralNumbers (0) | 2020.04.06 |
<Codesignal> File Naming (0) | 2020.04.06 |
<Codesignal> digitsProduct (0) | 2020.04.06 |
<Codesignal> Different Squares (0) | 2020.04.06 |