C - Type - Converting two uint8_t words into one of uint16_t and one of uint16_t into two of uint8_t

Sometimes you need to convert one uint16_t variable into two uint8_t. As we manipulate binaries, it’s impossible to do something like that: 0xAB + 0xCD = 0xABCD. Indeed, the result of 0xAB + 0xCD = 0x178 because 0xAB = 171 and 0xCD = 205. So 0xAB + 0xCD = 376 in decimal but 0x178 in hexadecimal. Furthermore, we cannot add more than 255 value inside a byte, because this is a 8-bit long number. ...

July 18, 2013 · Mi-K

C - Binary - Operation with decimals

A good way to understand binary operations is to play with decimals. What?! Yes, if you can manipulate decimals like binaries, then you can say “OK! I’ve understood how binary works”. Let’s check this with this binary-operation tutorial with decimal numbers. Explanation In the code below I’m starting with a XOR operation. I’m doing number XOR mask. number = 4. mask = 7. And the result is 3. What happened exactly? ...

February 18, 2013 · Mi-K

C - Type - What are uint8_t, uint16_t, uint32_t and uint64_t?

You are likely wondering what are uint8_t, uint16_t, uint32_t and uint64_t. That’s a good question. Because it could be really helpul! It turns out that they are equal respectively to: unsigned char, unsigned short, unsigned int and unsigned long long. But what are ranges of all these types? Let’s test it in this C type tutorial. Explanation We’re going to use a variable called testValue equal to 0xFFFFFFFFFFFFFFFF. Notice that 0xFFFFFFFFFFFFFFFF is the same as 18**,446,744,073,709,**551,615 and this is the maximum value possible for an unsigned long long, depending on your processor architecture (as gineera said in its comment). ...

February 14, 2013 · Mi-K

C - Tips'n Tricks - Creating a clock with six numbers after the decimal point

Everyone knows that a clock is essential in a video game. And the more the clock is precise, the more we can do things. That’s what we are going to see in this clock tutorial by creating a clock with six numbers after the decimal point. The code /* BadproG.com */ #include <stdlib.h> #include <stdio.h> #include <sys/time.h> #include <time.h> int clockTime(); int main(void) { if (clockTime()) return (1); return EXIT_SUCCESS; } /** * A clock with six numbers after the decimal point */ int clockTime() { time_t t; struct timeval tv; double gtod; double theTime; theTime = 0; gtod = 0; t = time(NULL); while (t < 2000000000) { t = time(NULL); gtod = gettimeofday(&tv, NULL); if (gtod != 0) { fprintf(stderr, "Error: clockTime().\n"); return (1); } theTime = (double)tv.tv_sec + (double)tv.tv_usec / 1000000; printf("theTime = %f\n", theTime); } return (0); } The result ... theTime = 1341489768.809641 theTime = 1341489768.809655 theTime = 1341489768.809670 theTime = 1341489768.809684 theTime = 1341489768.809698 theTime = 1341489768.809712 theTime = 1341489768.809726 theTime = 1341489768.809740 theTime = 1341489768.809755 theTime = 1341489768.809769 theTime = 1341489768.809783 theTime = 1341489768.809798 theTime = 1341489768.809812 theTime = 1341489768.809826 theTime = 1341489768.809840 theTime = 1341489768.809855 theTime = 1341489768.809869 theTime = 1341489768.809884 ... You can now check an action more quickly than before. You made it, well done.

July 5, 2012 · Mi-K

C - Tips'n Tricks - Retrieve temperature of hardware devices

The file from where we can retrieve temperature depends on the operating system. For this tutorial I used Ubuntu 12. The file on which I took temperature is /sys/class/thermal/thermal_zone8/temp. So you can test temperature of each file present in /sys/class/thermal/thermal_zone[0-9]/temp. On some GNU/Linux systems, files needed are sometimes in the /proc/acpi/thermal_zone/ directory. In the example below the heart of the code is in the main() function. All other functions are for testing that malloc(), open(), read(), close() and free() are called without any error. ...

July 5, 2012 · Mi-K

C - Mathematics - Using the Luhn algorithm

Maybe you wonder if your credit card has a correct number? How to know that? With the Luhn formula! This is what we are going to see in this Luhn tutorial. This algorithm can say if it is a good or a bad credit card number. Indeed, for that we first of all have to reverse the complete number. For example, the number to test is 1234567897. Once reversed, we have 7987654321. ...

April 24, 2012 · Mi-K

C - Mathematics - Find prime numbers

If you have a good computer, you can maybe try to find the last prime number that nobody found yet. Indeed, you can even win a prize for that! But you have to find a number with more than millions of digits. I’m sure you can do it. For the moment, let’s see how to find these numbers in this prime number tutorial. Arguments are a range: the minimum and the maximum numbers. ...

April 23, 2012 · Mi-K

C - Mathematics - Fraction simplifier

Today, I inaugurate a new C section: mathematics. The first tutorial is a way to simplify a fraction. I put all data in a structure that I typedefined as t_bp. I also made 3 files: main.c, misc.c and h.h. There is also an error manager inside the code. For example, the numbers allowed are 1 until 9. So if you type something else, it will generate an error. And of course, you have to enter 2 arguments: the numerator and the denominator. Notice that in this program you can use negative numbers. ...

April 23, 2012 · Mi-K

C - Library functions - Using inet_ntoa()

The inet_ntoa() function C returns the address of a client side, for example. We give to the unique inet_ntoa() parameter, a struct in_addr. We can then retrieve the address of this parameter. Let’s look it in this tutorial with an example of code. Using inet_ntoa() server.c /* server.c */ #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <arpa/inet.h> #include <netinet/in.h> #include <string.h> #include "h.h" void my_socket(t_s *s) { s->name = "TCP"; s->domain = AF_INET; s->type = SOCK_STREAM; s->pe = getprotobyname(s->name); s->fd = socket(s->domain, s->type, s->pe->p_proto); check_error(s->fd, -1); } void my_bind(t_s *s, int port) { s->sockfd = s->fd; s->addr.sin_family = s->domain; s->addr.sin_addr.s_addr = INADDR_ANY; s->addr.sin_port = htons(port); s->addrlen = sizeof(s->addr); s->bindValue = bind(s->fd, (const struct sockaddr *)&s->addr, s->addrlen); check_error(s->bindValue, -1); } void my_listen(t_s *s, int backlog) { s->listenValue = listen(s->sockfd, backlog); check_error(s->listenValue, -1); } void my_accept(t_s *s) { s->addrlenAccept = sizeof(s->addrAccept); s->acceptValue = accept(s->sockfd, (struct sockaddr *)&s->addrAccept, &s->addrlenAccept); check_error(s->acceptValue, -1); s->acceptAddr = inet_ntoa(s->addrAccept.sin_addr); check_null(s->acceptAddr); } int main(int ac, char *av[]) { t_s s; check_arg(ac, ARG_SIZE); my_socket(&s); my_bind(&s, atoi(av[1])); my_listen(&s, BACKLOG); my_accept(&s); debug(&s); check_error(close(s.acceptValue), -1); check_error(close(s.fd), -1); return 0; } debug.c /* debug.c */ #include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> #include <unistd.h> #include "h.h" void debug(t_s *s) { printf("\n===== socket() =====\n"); printf("domain = %d\n", s->domain); printf("type = %d\n", s->type); printf("fd = %d\n", s->fd); printf("name = %s\n", s->name); printf("p_proto = %d\n", s->pe->p_proto); printf("\n===== bind() =====\n"); printf("sockfd = %d\n", s->sockfd); printf("sin_family = %d\n", s->addr.sin_family); printf("sin_addr.s_addr = %d\n", s->addr.sin_addr.s_addr); printf("sin_port = %d\n", s->addr.sin_port); printf("addrlen = %d\n", s->addrlen); printf("bindValue = %d\n", s->bindValue); printf("\n===== listen() =====\n"); printf("listenValue = %d\n", s->listenValue); printf("\n===== accept() =====\n"); printf("addrlenAccept = %d\n", s->addrlenAccept); printf("acceptValue = %d\n", s->acceptValue); printf("s->acceptAddr = %s\n\n", s->acceptAddr); } void check_error(int test, int error) { if (test == error) { fprintf(stderr, "ERROR: %s\n", strerror(errno)); exit(EXIT_FAILURE); } } void check_null(char *test) { if (test == NULL) { fprintf(stderr, "ERROR: %s\n", strerror(errno)); exit(EXIT_FAILURE); } } void check_arg(int ac, int number) { if (ac < number) { printf("Usage: ./server [PORT]\n"); exit(EXIT_FAILURE); } } h.h #ifndef H_H_ #define H_H_ #include <netdb.h> /** * Define */ #define ARG_SIZE 2 #define BACKLOG 10 #define BUF_SIZE 255 /** * Structure */ typedef struct mystruct { /* socket */ int domain; int type; int fd; char *name; struct protoent *pe; /* bind */ int sockfd; socklen_t addrlen; struct sockaddr_in addr; int bindValue; /* listen */ int listenValue; /* accept */ int acceptValue; socklen_t addrlenAccept; struct sockaddr_in addrAccept; char *acceptAddr; /* read & write */ int readValue; int writeValue; char buf[256]; } t_s; /** * Prototype */ void debug(t_s *); void check_error(int, int); void check_null(char *); void check_arg(int, int); #endif /* H_H_ */ Compiling $ gcc server.c debug.c -o server ; ./server 5000 Now that the server is launched, it’s waiting a connection from a client on the port 5000. ...

April 20, 2012 · Mi-K

C - TCP/IP - Writing and reading on a socket

This tutorial will help us to understand how to write and read on a client side and display the result on the server side. So, for that, we have to create a server and a client. The client will be the nc tool (or telnet if you prefer, this is the same for this example). Let’s see this with an example. Writing and reading on a socket server.c /* server.c */ #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <arpa/inet.h> #include <netinet/in.h> #include <string.h> #include "h.h" void my_socket(t_s *s) { s->name = "TCP"; s->domain = AF_INET; s->type = SOCK_STREAM; s->pe = getprotobyname(s->name); s->fd = socket(s->domain, s->type, s->pe->p_proto); check_error(s->fd, -1); } void my_bind(t_s *s, int port) { s->sockfd = s->fd; s->addr.sin_family = s->domain; s->addr.sin_addr.s_addr = INADDR_ANY; s->addr.sin_port = htons(port); s->addrlen = sizeof(s->addr); s->bindValue = bind(s->fd, (const struct sockaddr *)&s->addr, s->addrlen); check_error(s->bindValue, -1); } void my_listen(t_s *s, int backlog) { s->listenValue = listen(s->sockfd, backlog); check_error(s->listenValue, -1); } void my_accept(t_s *s) { s->addrlenAccept = sizeof(s->addrAccept); s->acceptValue = accept(s->sockfd, (struct sockaddr *)&s->addrAccept, &s->addrlenAccept); check_error(s->acceptValue, -1); s->acceptAddr = inet_ntoa(s->addrAccept.sin_addr); check_null(s->acceptAddr); } void my_write(t_s *s) { const char *message = "Hello, please enter your name: "; s->writeValue = write(s->acceptValue, message, strlen(message)); check_error(s->writeValue, -1); } void my_read(t_s *s) { bzero(s->buf,256); s->readValue = read(s->acceptValue, s->buf, BUF_SIZE); check_error(s->readValue, -1); } int main(int ac, char *av[]) { t_s s; check_arg(ac, ARG_SIZE); my_socket(&s); my_bind(&s, atoi(av[1])); my_listen(&s, BACKLOG); my_accept(&s); my_write(&s); my_read(&s); debug(&s); check_error(close(s.acceptValue), -1); check_error(close(s.fd), -1); return 0; } debug.c /* debug.c */ #include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> #include <unistd.h> #include "h.h" void debug(t_s *s) { printf("\n===== socket() =====\n"); printf("domain = %d\n", s->domain); printf("type = %d\n", s->type); printf("fd = %d\n", s->fd); printf("name = %s\n", s->name); printf("p_proto = %d\n", s->pe->p_proto); printf("\n===== bind() =====\n"); printf("sockfd = %d\n", s->sockfd); printf("sin_family = %d\n", s->addr.sin_family); printf("sin_addr.s_addr = %d\n", s->addr.sin_addr.s_addr); printf("sin_port = %d\n", s->addr.sin_port); printf("addrlen = %d\n", s->addrlen); printf("bindValue = %d\n", s->bindValue); printf("\n===== listen() =====\n"); printf("listenValue = %d\n", s->listenValue); printf("\n===== accept() =====\n"); printf("addrlenAccept = %d\n", s->addrlenAccept); printf("acceptValue = %d\n", s->acceptValue); printf("s->acceptAddr = %s\n", s->acceptAddr); printf("\n===== write() & read() =====\n"); printf("s->buf = %s\n\n", s->buf); } void check_error(int test, int error) { if (test == error) { fprintf(stderr, "ERROR: %s\n", strerror(errno)); exit(EXIT_FAILURE); } } void check_null(char *test) { if (test == NULL) { fprintf(stderr, "ERROR: %s\n", strerror(errno)); exit(EXIT_FAILURE); } } void check_arg(int ac, int number) { if (ac < number) { printf("Usage: ./server [PORT]\n"); exit(EXIT_FAILURE); } } h.h #ifndef H_H_ #define H_H_ #include <netdb.h> /** * Define */ #define ARG_SIZE 2 #define BACKLOG 10 #define BUF_SIZE 255 /** * Structure */ typedef struct mystruct { /* socket */ int domain; int type; int fd; char *name; struct protoent *pe; /* bind */ int sockfd; socklen_t addrlen; struct sockaddr_in addr; int bindValue; /* listen */ int listenValue; /* accept */ int acceptValue; socklen_t addrlenAccept; struct sockaddr_in addrAccept; char *acceptAddr; /* read & write */ int readValue; int writeValue; char buf[256]; } t_s; /** * Prototype */ void debug(t_s *); void check_error(int, int); void check_null(char *); void check_arg(int, int); #endif /* H_H_ */ Compiling $ gcc server.c debug.c -o server ; ./server 5000 Now that the server is launched, it’s waiting a connection from a client on the port 5000. ...

April 20, 2012 · Mi-K