VB icon

Number Converter

Submitted on: 1/1/2015 9:42:00 AM
By: Josh Sherman (from psc cd)  
Level: Intermediate
User Rating: By 3 Users
Compatibility: PHP 4.0
Views: 3552
     This utility shows how to convert numbers to different types (i.e. hex, decimal, binary, octal).

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
// Name: Number Converter
// Description:This utility shows how to convert numbers to different types (i.e. hex, decimal, binary, octal).
// By: Josh Sherman (from psc cd)
// Assumes:The code converts the current value to a decimal, and then from decimal to the selected type. I do this because PHP has built in functions to convert hexadecimal, octal, or binary to decimal, and decimal to hexadecimal, octal, or binary, but doesn't including functions to convert octal to hexadecimal, binary to octal, or anything fancy like that. The code also makes use of the eval statement which helped reduce the length of the code considerably.

 * convert.php - Number conversion in PHP-GTK.
 * Author: Josh Sherman
 * Purpose: Converts a number to a different type.
 * Usage: php -q conversion.php
// Check to see if the PHP-GTK extension is available.
dl( 'php_gtk.' . (strstr( PHP_OS, 'WIN') ? 'dll' : 'so'));
// Called when delete-event takes place, tells it to proceed.
function delete_event()
	return false;
// Called when the window is being destroyed, tells it to quit the main loop.
function destroy()
// Called when a radio button is clicked, converts the number to that format.
function convert($widget, $which)
	global $current_type;
	global $entry;
	// Get the value from the entry field	
	$number = $entry->get_text();
	// Make sure they aren't clicking on an already active radio.
	if ($current_type != $which) {
		// Converts the number to decimal if it isn't already.
		if ($current_type != "dec") {
			eval ("\$number = " . $current_type . "dec(\"$number\");");
		// Converts the number to the desired format.
		if ($which != "dec") {
			eval ("\$number = strtoupper(dec" . $which . "(\"$number\"));");
		// Sets the entry box to the new value.
	// Set the new type as the current type.
	$current_type = $which;
// Creates a new top-level window and connect the signals to the appropriate functions.
$window = &new GtkWindow();
$window->connect('destroy', 'destroy');
$window->connect('delete-event', 'delete_event');
$window->set_title("Conversion Utility");
$window->set_policy(false, false, false);
// Creates a table to place our widgets, and adds it to the table.
$table = &new GtkTable(2, 1);
// Creates an entry field, and places it on our table.
$entry = &new GtkEntry();
$table->attach($entry, 0, 1, 0, 1);
// Creates another table, and places it on the existing table.
$types = &new GtkTable(1, 4);
$table->attach($types, 0, 1, 1, 2);
// Creates and groups radio buttons.
$hex = &new GtkRadioButton(null, 'Hex');
$dec = &new GtkRadioButton($hex, 'Dec');
$oct = &new GtkRadioButton($hex, 'Oct');
$bin = &new GtkRadioButton($hex, 'Bin');
// Set the 'Decimal' radio as active, and set the current type to decimal.
$current_type = "dec";
// Connect the radios to the convert function, and feeds the value to it.
$hex->connect('pressed', 'convert', 'hex');
$dec->connect('pressed', 'convert', 'dec');
$oct->connect('pressed', 'convert', 'oct');
$bin->connect('pressed', 'convert', 'bin');
// Place the radios on the table.
$types->attach($hex, 0, 1, 0, 1);
$types->attach($dec, 1, 2, 0, 1);
$types->attach($oct, 2, 3, 0, 1);
$types->attach($bin, 3, 4, 0, 1);
// Create tool tips for the widgets and enabled them.
$tthex = &new GtkTooltips();
$tthex->set_tip($hex, 'Convert the number to Hexadecimal.', '');
$ttdec = &new GtkTooltips();
$ttdec->set_tip($dec, 'Convert the number to Decimal.', '');
$ttoct = &new GtkTooltips();
$ttoct->set_tip($oct, 'Convert the number to Octal.', '');
$ttbin = &new GtkTooltips();
$ttbin->set_tip($bin, 'Convert the number to Binary.', '');
$ttentry = &new GtkTooltips();
$ttentry->set_tip($entry, 'Type the number you want to convert here.', '');
// Show the window and all of it's child widgets.
// Run the main loop.

Other 12 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.