Category Archives: Tips & Tricks

String to Hex & Hex to String Conversion in PHP

Hi Guys I just gone through a situation need to convert string to Hex but unfortunatly not found any Pre-build function in PHP but don’t worry today I would like to share you a function which solve the purpose in either way

String to Hex Function

/**
 * CodeZone.in
 * Author: Dineshkumar
 * Contact : hi@codezone.in
 */
 function string2hex($string){
    $hex = '';
    for ($i=0; $i<strlen($string); $i++){
        $ord = ord($string[$i]);
        $hexCode = dechex($ord);
        $hex .= substr('0'.$hexCode, -2);
    }
    return strToUpper($hex);
}

Hex to String Function

/**
 * CodeZone.in
 * Author: Dineshkumar
 * Contact : hi@codezone.in
 */
 function hex2string($hex){
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2){
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}

Usage:

Include function in your PHP script and call them with required parameters to get your converted output.

Happy Coding….

ASCII to HEX Convertion in PHP

Hi guys.. I was searching for function to convert ASCII to HEX in PHP accidentally there is no pre-build function so today I am going to share you a function which will do this conversation.

/**
 * CodeZone.in
 * Author: Dineshkumar
 * Contact : hi@codezone.in
 */
 function ascii2hex($ascii) {
  $hex = '';
  for ($i = 0; $i < strlen($ascii); $i++) {
    $byte = strtoupper(dechex(ord($ascii{$i})));
    $byte = str_repeat('0', 2 - strlen($byte)).$byte;
    $hex.=$byte." ";
  }
  return $hex;
}

Happy coding….

How to enable Hibernate in Ubuntu Linux

Actually I am a multi-tasker and most of the programmers will do. When I was in windows I used to hibernate my system so that I can start from where I left. But after I moved to Ubuntu 16.04 I was not able to find that option in menu and was wondering about it. Then when I searched for solution I found that Ubuntu disabled that option since some programs not support hibernate option. Any how today we are going to see steps to hibernate your system as well as how to enable menu option in you Ubuntu OS.

First of all what happens when hibernate? Actually all data in RAM is stored in swap partition during computers gets off and then it gets retrieve back to RAM when computers gets on. During hibernate in laptops no hardware uses power resources.

First we need to analyse whether hardware support is there.

  1. Open Terminal using ctrl+Alt+t
  2. Type following code
/**
 * CodeZone.in
 * Author: Dineshkumar
 * Contact : hi@codezone.in
 */
 sudo pm-hibernate

Right now you computer gets turns off. Switch on back and check whether all your application gets re-open. If all programs are running then Hibernate working properly.

If hibernate doesn’t work, A common problem is size of swap partition. As i said before RAM contents stored in swap partition so it is mandatory that swap partition is at least as large as your available RAM.

To check this go to system monitor

ubuntu-systemmonitor

If Swap is less then memory and you wish to increase SWAP memory then you can do that using gparted live CD.

So Now we need to enable Hibernate option in menu. For that open terminal and run following commands

/**
 * CodeZone.in
 * Author: Dineshkumar
 * Contact : hi@codezone.in
 */
 
$ sudo -i

$ cd /var/lib/polkit-1/localauthority/50-local.d/

$ gedit com.ubuntu.enable-hibernate.pkla

Copy and paste following codes for Ubuntu 16.04

[Re-enable hibernate by default in upower]
Identity=unix-user:*
Action=org.freedesktop.upower.hibernate
ResultActive=yes

[Re-enable hibernate by default in logind]
Identity=unix-user:*
Action=org.freedesktop.login1.hibernate;org.freedesktop.login1.handle-hibernate-key;org.freedesktop.login1;org.freedesktop.login1.hibernate-multiple-sessions;org.freedesktop.login1.hibernate-ignore-inhibit
ResultActive=yes

Logout and restart your system.

After you log into system you will find hibernate in system menu.

Screenshot 2016-09-06 13:38:49

 

Simple Site Maintenance HTML Page

There is a situation for all need to make their site under Maintenance or under construction. Many are providing so many templates and I also saw many used to have images in their webpages. I was wondering why it can be a simple HTML page. So I am sharing you a simple HTML script will take care of it.

/**
 * CodeZone.in
 * Author: Dineshkumar
 * Contact : hi@codezone.in
 */
<!doctype html>
<title>Site Maintenance</title>
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%3E%0A%20%20body%20%7B%20text-align%3A%20center%3B%20padding%3A%20150px%3B%20%7D%0A%20%20h1%20%7B%20font-size%3A%2050px%3B%20%7D%0A%20%20body%20%7B%20font%3A%2020px%20Helvetica%2C%20sans-serif%3B%20color%3A%20%23333%3B%20%7D%0A%20%20article%20%7B%20display%3A%20block%3B%20text-align%3A%20left%3B%20width%3A%20650px%3B%20margin%3A%200%20auto%3B%20%7D%0A%20%20a%20%7B%20color%3A%20%23dc8100%3B%20text-decoration%3A%20none%3B%20%7D%0A%20%20a%3Ahover%20%7B%20color%3A%20%23333%3B%20text-decoration%3A%20none%3B%20%7D%0A%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<style>" title="<style>" />

<article>
    <h1>We’ll be back soon!</h1>
    <div>
        <p>Sorry for the inconvenience but we’re performing some maintenance at the moment. If you need to you can always <a href="mailto:#">contact us</a>, otherwise we’ll be back online shortly!</p>
        <p>— The Team</p>
    </div>
</article>

Just Change # in above code to your email address.

Happy coding…