Archive for February, 2012

[C] Learning Byte and Bit in Language C

Wednesday, February 22nd, 2012

I am learning developing in C now. So I have to understand byte and bit.
As I recognized it, I will write down about these things. I hope to help you to understand it.

Bit

A bit represents 0 or 1. It usually used as base-2 number system.
Here is an example table.

Binary Decimal Number of bit
0 0 1
1 1 1
10 2 2
11 3 2
100 4 3
111 7 3
1000 8 4
1010 10 4
1111 15 4

You found that 4 bit can represent from 0 to 15 in decimal.
So it can represents type of 16 number.

By the way hexadecimal is base-16 number system.
In C, it is written with prefix 0x. Let’s add hexadecimal column to above table.

Binary Decimal Hexadecimal Number of bit
0 0 0x0 1
1 1 0x1 1
10 2 0x2 2
11 3 0x3 2
100 4 0x4 3
111 7 0x7 3
1000 8 0x8 4
1010 10 0xA 4
1111 15 0xF 4

Byte

Byte usually represents 8 bit.
Can you understand how it can represents decimal range?
I said 4 bit represents from 0 to 15 In previous paragraph.
As 8 bit equals 4 bit add 4 bit, it is calculated by below expression.
16 * 16 = 256
So 8 bit can represent from 0 to 255 in decimal, and type of 256 number.

Let’s expand previous table until 8 bit.

Binary Decimal Hexadecimal Number of bit
0 0 0x00 1
1 1 0x01 1
10 2 0x02 2
11 3 0x03 2
100 4 0x04 3
111 7 0x07 3
1000 8 0x08 4
1010 10 0x0A 4
1111 15 0x0F 4
10000 16 0x10 5
10001 17 0x11 5
11111 31 0x1F 5
100000 32 0x20 6
111111 63 0x3F 6
1000000 64 0x40 7
1111111 127 0x7F 7
10000000 128 0x80 8
11111111 255 0xFF 8

Mmm, it seems difficult at a glance. But it is easy to understand if you read it little by little.
Let’s look into decimal 64.

Binary Decimal Hexadecimal Number of bit
1000000 64 0x40 7

First I split binary to two parts. I make each part 4 bit.
100 – 0000
The left part is 100. It is represented 0x4 in hexadecimal.
The right part is 0000. It is represented 0x0 in hexadecimal.
So in hexadecimal, it is represented 0x40.

ASCII Table

ASCII table is character code table.
>> ASCII Table and Description

Let’s add this ASCII code column to previous table we made.

Binary Decimal Hexadecimal ASCII character Number of bit
0 0 0x00 NUL 1
1 1 0x01 SOH 1
10 2 0x02 STX 2
11 3 0x03 ETX 2
100 4 0x04 EOT 3
111 7 0x07 BEL 3
1000 8 0x08 BS 4
1010 10 0x0A LF 4
1111 15 0x0F SI 4
10000 16 0x10 DLE 5
10001 17 0x11 DC1 5
11111 31 0x1F US 5
100000 32 0x20 Space 6
111111 63 0x3F ? 6
1000000 64 0x40 @ 7
1000001 65 0x41 A 7
1010001 97 0x61 a 7
1111111 127 0x7F DEL 7
10000000 128 0x80 8
11111111 255 0xFF 8

Over decimal 127, there seem to be several type of extended ASCII table.
So I didn’t write character.

Let’s look into row of character ‘A’.

Binary Decimal Hexadecimal ASCII character Number of bit
1000001 65 0x41 A 7

Character ‘A’ is equal to decimal 65, and hexadecimal 0x41.

Character and int in language C

Previous paragraph shows you relationship between character code and decimal and hexadecimal.
I show you simple C code.

#include <stdio.h>

int main(int argc, char const *argv[])
{
	char c, c2;
	int i, j;

	c = 'A';
	printf("c is %c. character code is %d, binary is %x\n", c, c, c);
	//output
	//c is A. character code is 65, binary is 41

	i = 97;
	printf("i is %c. character code is %d, binary is %x\n", i, i, i);
	//output
	//i is a. character code is 97, binary is 61

	//char to int
	j = c;
	printf("j is %c. character code is %d, binary is %x\n", j, j, j);
	//output
	//j is A. character code is 65, binary is 41

	//int to char
	c2 = i;
	printf("c2 is %c. character code is %d, binary is %x\n", c2, c2, c2);
	//output
	//c2 is a. character code is 97, binary is 61

	return 0;
}

Over One Byte

You can understand one byte. But it seems you have to use more than one byte in ordinary programming.
So I am going to write down it on next article.

[SQLite3] SQLite on Mac and compile with gcc

Wednesday, February 8th, 2012

I needed to learn SQLite for my client work and had enjoyed it.

on terminal

Fortunately SQLite is pre installed in mac OS.

So you can create database instantly.
This code creates database named hello.db.
But actual file won’t create if you don’t execute any SQL command.

sqlite3 hello.db

If you finish sqlite, type “.exit” .

You can configure settings of SQLite with using ~/.sqliterc .

.echo ON
.mode column
.headers ON
.nullvalue "NULL"

If you put this file, sqlite start with these settings.

Create table.

sqlite> create table hello (id integer primary key, message text);

Insert row.

sqlite> insert into hello (message) values ('hello');

You have to be careful typing string literal.
In SQLite string literal is recommended to delimite with single quotes.

Select data

sqlite> select * from hello;

OK. Although these are the most basic SQLite command in terminal, these are enough to develop.

Let’s start to develop in C.

SQLite with gcc

I am interested in gcc. Because I wonder if I don’t have to use XCode when I compile C.

mysqlite1.c

#include <stdio.h>
#include <string.h>
#include "/usr/include/sqlite3.h"

int main(){
	sqlite3 *db;
	int result, id;
	sqlite3_stmt *stmt;
	char *sql, *name;

	result = sqlite3_open_v2("hello.db", &db, SQLITE_OPEN_READONLY, NULL);
	if(result != SQLITE_OK){
		printf("database opened\n");
		sqlite3_close(db);
		return 1;
	}

	sql = "select * from hello;";
	result = sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL);

	if(result != SQLITE_OK){
		printf("statement error");
		return 1;
	}

	result = sqlite3_step(stmt);
	while(result == SQLITE_ROW){
		id = sqlite3_column_int(stmt, 0);
		name = (char *)sqlite3_column_text(stmt, 1);
		printf("id = %d, name = %s\n", id, name);
		result = sqlite3_step(stmt);
	}
	sqlite3_finalize(stmt);
	sqlite3_close(db);
	return 0;
}

As mac has sqlite3 library, we can include sqlite’s header file.
#include “/usr/include/sqlite3.h”

Let’s compile it in terminal.
If the compiling succeseed, there is a file named mysqlite1.

gcc -Wall -L/usr/lib/sqlite3 mysqlite1.c -lsqlite3 -o mysqlite1

Test executing.

./mysqlite1

We can also compile sources with makefile.
Makefile is convenient way to compile multiple sources or link library.

If you make makefile, then it may be this code.

makefile

CC=gcc
CFLAGS=-Wall
all: mysqlite1

mysqlite1: mysqlite1.o
	$(CC) $(CFLAGS) -L/usr/lib/sqlite3 mysqlite1.o -lsqlite3 -o mysqlite1

mysqlite1.o: mysqlite1.c
	$(CC) $(CFLAGS) -c mysqlite1.c

clean:
	rm -f *.o mysqlite1

After you put this file named makefile, execute this command.

make

If the command execute successfully, mysqlite1 exists in current directory.
And if “clean” command executed, it removes object files( *.o) and mysqlite1.

make clean

Furthermore

This article is based on these books.
>> The Definitive Guide to SQLite
>>An Introduction to GCC: For the GNU Compilers GCC and G++

And these sites.
>> Makefiles by example