Today I will show you how to transform a decimal number, an hexadecimal number and a binary number into the same character.
And you will see that this transformation is made by the terminal, the shell and Linux (or UNIX ;).
Let's see an example, with 3 files and after with printf():
First of all, you have to create 3 files named file1, file2 and file3.
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { int fd1; int fd2; int fd3; char i; char j; char k; i = 120; j = 0x78; k = 0b01111000; fd1 = open("file1", O_WRONLY); fd2 = open("file2", O_WRONLY); fd3 = open("file3", O_WRONLY); write(fd1, &i, 1); write(fd2, &j, 1); write(fd3, &k, 1); }
Compile and execute it:
$ cc main.c && ./a.out
Open now your all 3 files, and you will see the x character inside!!
Incredible, isn't it?
Why?
Because your shell asks Linux which character has to be used and tells to the terminal which character display.
If you prefer to see this example directly in your terminal, let's use the printf() function:
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { int fd1; int fd2; int fd3; char i; char j; char k; i = 120; j = 0x78; k = 0b01111000; printf("Character = %c\n", i); printf("Character = %c\n", j); printf("Character = %c\n", k); }
Let's compile and execute it.
The result:
Character = x Character = x Character = x
Add new comment