[C] Learning Byte and Bit in Language C

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 0×0 1
1 1 0×1 1
10 2 0×2 2
11 3 0×3 2
100 4 0×4 3
111 7 0×7 3
1000 8 0×8 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 0×00 1
1 1 0×01 1
10 2 0×02 2
11 3 0×03 2
100 4 0×04 3
111 7 0×07 3
1000 8 0×08 4
1010 10 0×0A 4
1111 15 0×0F 4
10000 16 0×10 5
10001 17 0×11 5
11111 31 0×1F 5
100000 32 0×20 6
111111 63 0×3F 6
1000000 64 0×40 7
1111111 127 0×7F 7
10000000 128 0×80 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 0×40 7

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

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 0×00 NUL 1
1 1 0×01 SOH 1
10 2 0×02 STX 2
11 3 0×03 ETX 2
100 4 0×04 EOT 3
111 7 0×07 BEL 3
1000 8 0×08 BS 4
1010 10 0×0A LF 4
1111 15 0×0F SI 4
10000 16 0×10 DLE 5
10001 17 0×11 DC1 5
11111 31 0×1F US 5
100000 32 0×20 Space 6
111111 63 0×3F ? 6
1000000 64 0×40 @ 7
1000001 65 0×41 A 7
1010001 97 0×61 a 7
1111111 127 0×7F DEL 7
10000000 128 0×80 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 0×41 A 7

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

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

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

[Dialy] Sublime Text 2 is a good editor

January 30th, 2012

I bought Sublime Text 2. It is a good editor.
I knew it on Keith Peters’s blog post.

I had tried it for a week. And I like it very much. My favorite points are ..

1. Multibyte support
2. Short application launch time
3. Customizable ( by Python )

1. is very important point for me. I sometimes input Japanese characters into source code. But some editor don’t work correctly. For example, they display wrong character, or can’t input character when I convert to Kanji.

As I like customizing application, I am attracted the feature(3.). But I don’t know how to configure it yet. If I have a time, I will learn it.

[terminal] archive file to zip format with password on terminal

December 30th, 2011

We can archive file to zip format on terminal without any other app.

1. archive only a file

zip example.zip filename

2. archive directory

zip -r example.zip directoryname

3. archive directory with password

zip -P yourpassword -r example.zip directoryname

[iOS] CSS3 animation is stopped when I scroll my safari on iPad.

December 26th, 2011

I wrote CSS3 animation code like this, and apply it to certain element.

@-webkit-keyframes flapping {
   0% {
   	-webkit-transform:scale(0,0);
   	opacity:1;
   }
   100% {
   	-webkit-transform:scale(1,1);
   	opacity:0;
   }
}

But CSS3 animation is stopped when I scroll my safari on iPad.
Accurately saying, it stops on 0% position of keyframes, and doesn’t progress further more.

I googled. Then I got a result which may resolve this problem.

>> GPUアクセラレーターが使える環境で強制的に有効にできるCSSの指定方法

It says that mobile safari is flickered when safari changes CPU render to GPU one.
If safari initially uses GPU rendering, it doesn’t flicker.
To do this, write this css property to target element.

-webkit-transform-style: preserve-3d;

Then I fixed the problem by this way.
Thanks.

I think that safari may change render method when it scrolls.

[terminal] recursively delete .DS_Store in terminal

December 24th, 2011

I noticed that my git repository has .DS_Store files.
The .DS_Store file is used by Mac OS.

As I wanted not to include these files in my repository,
I tried to delete these as follow way.

First, I deleted all .DS_Store files in the directory recursively on terminal.

find . -name '.DS_Store' | xargs rm

Next, I created ~/.gitignore_global file.

update 26th Dec. 2011
I have to type this command

git config --global core.excludesfile ~/.gitignore_global

I wrote these strings in it.
The file is global settings in git.
If you specify a setting in certain directory, you can create .gitignore file in the directory.
It stops to add to stage of git the files in which are wrote.

# OS generated files #
######################
.DS_Store*
ehthumbs.db
Icon?
Thumbs.db

Finally I committed this state.

I referred these sites. Thanks.
>> フォルダ以下を再帰的にgrep検索する方法 – gorton-lab 日記の下書き
>> Help.GitHub – Ignore files

[JavaScript] 3D on Canvas2D

December 20th, 2011

I am learning 3D basic theory again.


I have been using these books.
They are very kind for beginners like me.

>> Beginning Math and Physics for Game Programmers [Paperback]
>> Foundation ActionScript 3.0 Animation: Making Things Move! [Paperback]

Although I have learned 3D basic theory in AS3 before, I am trying to implement it by JavaScript.

I wrote above demo by this way.
I wrote CoffeeScript first.
Then it is compiled to js file by Cakefile which watches the .coffee files in certain directory.

[JavaScript] watch and compile, then concatenate them in CoffeeScript Cakefile

December 17th, 2011

I am learning CoffeeScript.
I saw Cakefile, and learned it.
I found that it behaves like batch file.

Then I wanted it to use as these way.

1. It keeps watching src/*.coffee files
2. When they are changed, it compiles them
3. It concatenates compiled files to lib/main.js

I made below code.
Although I don’t know whether it is correct or not, it works.

Cakefile

{spawn} = require 'child_process'

task 'watch', 'watch src/ and concatenate them to lib/main.js', (callback) ->
  watch = spawn 'coffee', ['-w', 'src/']
  watch.stderr.on 'data', (data) ->
    process.stderr.write data.toString()
  watch.stdout.on 'data', (data) ->
    console.log 'file changed'
    build = spawn 'coffee', ['-j', 'lib/main.js', '-cl', 'src/']
    build.stderr.on 'data', (data) ->
      process.stderr.write data.toString()
    build.on 'exit', (code) ->
      if code is 0
        console.log 'build complete'

How to use
In terminal type this

cake watch

I refered these pages. Thanks.

>> node.js – coffee script cakefile task not finishing – Stack Overflow
>> [HowTo] Compiling and Setting Up Build Tools – GitHub
>> The Little Book on CoffeeScript – Compiling
>> Cakefile(zsh) – podhmoの日記
>> CakeFileの使い方 -CoffeeScript- – プログラムdeタマゴ

[JavaScript] ratween CSS3 Transition jQuery plugin

December 13th, 2011

I made ratween jQuery plugin .
This plugin makes elements CSS3 transition.

See demo

>>Download it from github.

It’s very convenient plugin when you develop webkit browser in iOS.
Because CSS3 transition is faster than normal css tween.

These pages contains very useful information.
Thank you for writing.

>> Plugins/Authoring – jQuery JavaScript Library
>> Detecting and generating CSS animations in JavaScript ✩ Mozilla Hacks – the Web developer blog
>> CSS transitions – MDN
>> Visualize and manipulate CSS transition functions

[Android] how to include your app into list in dialog which is launched when you put device on cradle or dock

December 10th, 2011

I am very in trouble not to find how to solve the problem.
The problem is that I want to include my app into list in dialog.
The dialog is launched when I put device on cradle or dock.

I have searched for a very long time.
And finally I found it!

The hint page is here. I appreciate the author writing it very much.
Custom CAR DOCK application in Android

I have to use intent. And the setting is following.

AndroidManifest.xml

            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.DESK_DOCK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

This resolve point is I have to use both category.DESK_DOCK and category.DEFAULT together.
If I use only which one of those, it can’t work.

Here is the screenshot.
DeskDockTest app is my application.
The others are default app of devices.