• missing xbfish.com image

Category Archives: Programming

Integer to Binary Conversion Part 2

This post is a continue from the previous post Integer to Binary Conversion Part 1.

missing xbfish.com image

Enhancement and Fix:
- Adding a menu system
- Overflow check

The code in C# :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
 
class Program {
    static void Main(string[] args) {
        int num = 0,  temp = 0, i = 0, menu = 0;
        int[] result = new int[8];
 
	printMenu();
        menu = readInt();
 
	while(menu != 0){
	   Console.WriteLine("Enter an integer to convert to binary:");
           num = readInt();
 
           if (num > 255) {
		Console.WriteLine("The integer exceeds the limit of 8 bits binary");
	   }else {
		Console.WriteLine("Converting..... The result is:");
		while (num != 0) {
		   temp = num % 2;
		   result[i] = temp;
		   num /= 2;
		   i++;
		}
		printResult(i, result);
	   } 
	   i = 0;
	   printMenu();
	   menu = readInt();
	}
    }
 
    public static int readInt(){
	return int.Parse(Console.ReadLine());
    }
 
    public static void printMenu() {
	Console.WriteLine("============ INTEGER TO BINARY CONVERTER  =============");
	Console.WriteLine("1. Press any number to convert Integer to 8 bits binary");
	Console.WriteLine("2. Press 0 to exit");
	Console.WriteLine("=======================================================");
	Console.WriteLine();
    }
 
    public static void printResult(int i, int []array) {
	Console.WriteLine("The 8 bits binary is: ");
	for(int k = (i - 1); k >= 0; k--){
            Console.Write(array[k]);
        }
	Console.WriteLine();
	Console.WriteLine();
    }
}

I will try to improve the code snippet when I have the time again… :)

Styling input field when focus using CSS

Conventionally, the default styling for input can be ugly when you tried to click on it. Example of an default input field without any styling:

Example 1:

Now, lets give a “life” to the input field by styling it using CSS:

1
2
3
4
input:focus{
background-color: lightyellow;
border: 1px solid #CCC;
}

By styling all input fields, we can achieve the following effect when the textbox is being focus (or being click onto):

Example 2:

Notice the difference when you click on both of the textbox?

I hope this helps :)

Integer to Binary Converter Part 1

Out of boredom, I write a code to convert Integer to Binary in C# :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
 
class Program {
    static void Main(string[] args) {
        int num = 0,  temp = 0, i = 0;
        int[] result = new int[8];
 
        Console.WriteLine("Enter an integer to convert to binary:");
        num = Int16.Parse(Console.ReadLine());
 
        Console.WriteLine("Converting..... The result is:");
        while (num != 0) {
            temp = num % 2;
            result[i] = temp;
            num /= 2; 
            i++;
        }
 
        // Printing out the answer 
        for(int k = (i - 1); k >= 0; k--){
            Console.Write(result[k]);
        }
    }
}

As you can see, the code is relatively simple and doesn’t have any validation check for overflow. The shortfall of this code is that the largest binary that can be converted from integer is 1111 1111.

I will brush up the code when I am free again.

** Refer to the post Integer to Binary Conversion Part 2 for the improved code snippet.

[Prestashop] Prestashop Cookie Structure

Prestashop cookie structure provided by nethercottconstructions is very useful if you are developing your own plugins or customizing the application.

To summarize, if you are going to access cookie from the inside of Prestashop, write the following line to the top of the function in a class or at the top of a non-class file:

global $cookie;

If you are accessing from the outside of Prestashop, write the following line to the top of the function in a class or at the top of a non-class file:

1
2
3
4
include_once('path_to_prestashop/config/config.inc.php');
include_once('path_to_prestashop/config/settings.inc.php');
include_once('path_to_prestashop/classes/Cookie.php');
$cookie = new Cookie('ps');

Note :: Change ‘ps’ to ‘psAdmin’ to read the employee/administrator cookie.

You can then access the data in the cookie calling the variable:

$cookie->variable

For a list of cookie variables, refer to their cookie structure page.

Source : http://www.nethercottconstructions.com/en/content/53-prestashop-cookie-structure

Redirect URLs using .htaccess

Suppose you want to redirect http://example.com/folder to http://anotherfolder.example.com, you can add the following line in your .htaccess file:

Redirect permanent /folder http://anotherfolder.example.com

Reference :: http://www.besthostratings.com/articles/htaccess-redirects.html

[CakePHP 2.0] Authenticating user with email

By default in CakePHP 2.0, the authentication component make use of 2 fields to authenticate a user: username & password

If your web application requires user to login using email instead of a username, you can add in the following code snippet in the respective controller:

1
2
3
4
5
6
7
8
public $components = array(
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email')
            )
        )
    )
);

Take note that $components is a variable instead of a function under a controller.

Explanation: In the above code snippet, the email posted by the form will be taken as the username for the authentication component. The authentication component will then authenticate the user using email when $this->Auth->login() is called.

For more info, please refer to CakePHP 2.0 authentication component.

Checking JDK / JRE bit version on Windows

To check Java Development Kit (JDK) or Java Runtime Environment (JRE) bit version on Windows, open up command prompt and enter the following command:

java -version

If it is 32-bit, you should see something embedded in the version info: Java HotSpot(TM) Client VM

If it is 64-bit, you should see something embedded in the version info: Java HotSpot(TM) 64-bit Server VM

This information is gathered from Java 6. I am not sure this is applicable to Java 7. If anyone knows, please comment. :)

Create new Rails application with MySQL as default db driver

To create a new Ruby on Rails application with MySQL as the default db driver in database.yaml:

rails new app -d mysql

Add -d mysql at the end of the command.

[WordPress] Version 3.3 Admin Toolbar

After updating to WordPress Version 3.3, the admin toolbar is now different from version 3.2.x

Left side of the admin toolbar:

xbfish.com

Right side of the admin toolbar:

xbfish.com

The admin toolbar is now neater and nicer looking.

Installing Rails Node.js in Ubuntu

I faced with the following error when bundling install a Ruby on Rails application:

missing xbfish.com image

I already had mysql2 installed and the error turned out that my development environment is lacking of Node.js

To install Node.js in Ubuntu, open up your Terminal and enter the following command:

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

For other Linux distro installation, refer to Node.js @ GitHub.