$string = "10101011";echo(stringbin2num($string)); // Will return 171function stringbin2num($input) { $len = strlen($input); $count = 0; $val = 0; while ($count < $len) { if (substr($input, $len-$count-1, 1)) { $val += pow(2, ($count)); } $count++; } return $val;}This function might not be needed in many situations. It's just a reference for those who want to convert string to numbers. You can also modify...
Tuesday, November 29, 2011
PHP to Write Binary Data (or Array Data) to a File using Pack Function
Writing the Binary Data directly using PHP's Pack() function:// Write Binary Data$bin_data = pack("C*", 0x47, 0x49, 0x46);$fp = fopen("c://test.bin", 'wb'); // replace with your filename // (please also check file permission before write)fwrite($fp, $bin_data);fclose($fp);Writing the Binary Data from an Array (if the data length is too long):// Write Binary Data$bin_data_array = array(0x47, 0x49, 0x46, .....................................);foreach ($bin_data_array...
Facebook Funny Little Trick
There are numerous posts I find it funny.It is using the Facebook wall to post something funny using the UID such as@[84786553145:0]After you paste on Facebook wall and it will appear as "Kiss Me!".It's actually showing the username of the Facebook page "Kiss Me!" and people may not be aware of this when they paste it onto their wall.But you need to add something like '+' sign to something like @+[84786553145:0] when you send the instruction to paste it to your friends wall and tell them...
Tuesday, November 22, 2011
Global Variable/Array in PHP
The global variable is declared in the function instead of in the main as in C language.<?PHP$my_global_array = array(); // Global array set up in main$my_global_variable; // Global variable set up in main...funtion a() { global $my_global_array, $my_global_variable;// declare the array and variable in main as global variable...}function b() { global $my_global_array; // only takes this global array as the global variable is not needed in this function...}?&...
Monday, November 21, 2011
CSS font used by Facebook
Now the fonts used by Facebook become the favorite for many. If you do a view source, you'll see this CSS setting:font-family:"lucida grande",tahoma,verdana,arial,sans-serif;font-size: 11px;The color tone is not entirely black and it is a bit towards gr...
PHP: Setting and Reading Cookies
<?PHPsetcookie("myCookieName", "myCookieValue"); // To read cookie, simply retrieve from the $_COOKIE['myCookieNameToRead'] ...?><HTML>..IMPORTANT: Make sure your <PHP is the first thing is your PHP file. Even a single tiny white space character prior to <PHP will also render the cookie operation usele...
PHP: $_REQUEST or $_POST or $_GET?
If you are very sure of the return string type, use $_POST['myStringKey'] for POST data and $_GET['myStringKey'] for GET data.If you are not really sure (ex. it's from a third party source such as Facebook), use $_REQUEST['myStringKey']....
PHP Weird Initialization Criteria
It is weird that before pushing data to an array, one needs to initialize the array before that.Here's what I did to avoid this weird error (pushed data will be missing):$my_array = array(); // initialize $my_array is of array typearray_push($my_array, "myTestData1");If you are used to PERL, this is wei...
The Unbelievable PHP Debug Feature: print_r()
There is a function that I personally like very much: print_r().It prints out the raw data inside a string or array. Especially when dealing nested arrays. It is a function I cannot live without.When dealing JSON data return from Facebook API/Graph, print_r() can be a very powerful debugging feature especially when the returning data structure is unknown.Here's an example of examining data returned from Facebook FQL engine:// Get your access token ($access_token) from OAuth 2.0 procedure$fql_query_url...
PHP Codes to Force a Number to Be a Negative Number
$number = ~abs($number) + 1;~ is to invert all the bits in PHP and + 1 is to make it a negative number. Remember in CS101 that binary number can be converted to represent a negative number by invert all the bits and +1? abs() is to make sure it is not a negative number and force it to be a positive number even though it is a negative number. Inverting a negative number will only make it wor...
Use JQUERY to blink Texts
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script><script> function startBlink(){ window.blinker = setInterval(function(){ if (window.blink) { $('.blink').css('color','black'); window.blink=false; } else { $('.blink').css('color','white'); window.blink = true; } },500); }startBlink();</script>...<span class="blink"> Your texts here! </span>This...
Sunday, November 20, 2011
The Facebook Avatar Thumb Nail URL
http://graph.facebook.com/[Facebook Profile ID]/picture
This link will be redirected to the actual jpg file in one of the servers of facebo...
PHP Code to Convert Unsigned Word (Double Byte, 16-bit String) to Signed Integer
This is useful when one tries to read a two 8-bit unsigned char from binary file and then convert to a signed integer.
The following function is good to convert from 2 character byte string such as 0A, FE, etc., to a signed integer (tested working on both 32-bit and 64-bit Windows 7 IIS environment):
function read_short($s) { // assume $s is an array of (char 1, char 2)
$temp = (($s[0]&0x00ff)<<8) + ($s[1]&0x00ff);
if ($temp & (1 << 15) ) {
$temp = ($temp...
Subscribe to:
Posts (Atom)