This article is a look up table example.
The example program receives a signed integer and presents at Hyperterminal its hexadecimal equivalent.
Casting the address of the integer to point to a new type like:
typedef struct s {
unsigned int n0 :4;
unsigned int n1 :4 ;
unsigned int n2 :4 ;
unsigned int n3 :4;
} nibbles;
we can take each one of the four nibbles of the 16 bit integer writing for example
((nibbles*)(&x))->n2
After it we have to find strings that present the nibbles (the integers n0...n3) as "the nibbles are".
To do it we have to look in a look up table like:
char* table [16]={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}; // the look up table
And finaly we can print the four nibbles as strings writing:
printf ("Your integer in hexadecimal is :%s%s%s%s \n",table[((nibbles*)(&x))->n3], table[((nibbles*)(&x))->n2] , table[((nibbles*)(&x))->n1], table[((nibbles*)(&x))->n0] );
The c code The result
Exercise1 : Replace each one of the sixteen hexadecimal digits of the look up table with its binary equivalent and find the binary equivalent of the recieved integer.
Exercice2 : Rewrite the above program sending now a float number and presenting its hexadecimal equivalent at hyperterminal.
Help: You have to define a new type as the structure s above, but now you have to include in it 8 bitfields of unsigned indegers, each one of them must be 4 bit long , because a float is 4 bytes long.
Remember: The low byte is saved always first in the memory space! Our machine is a little endian machine.
Important : If you are not sure "how the things are defined" then have a look inside of the main.h file.
|