Tag Archive for convert

Object to array

function object_to_array($object){
	$new = NULL;
	if (is_object($object)) {
		$object = (array) $object;
	}
	if (is_array($object)) {
		$new = array();
		foreach ($object as $key => $val) {
			$key = preg_replace("/^(.*)/", "", $key);
			$new[$key] = $this->object_to_array($val);
		}
	} else {
		$new = $object;
	}
	return $new;
}

source

convert int to binary in c

#define WIDTH 32

const char *int_to_binary ( int x ) 
{ 
	static char b[WIDTH + 1] = {0};
	for (int z = WIDTH; z > 0; z--) 
	{ 
		b[z - 1] = ((1 & x) == 1) ? '1' : '0'; 
		x >>= 1; 
	}
	return b; 
}

int main() 
{ 
	unsigned int i = 0; 
	i = ~i; 
	printf("%s
", int_to_binary(i)); 
	return 0; 
}

source

UTF to HTML

function utf8_to_html($data) {
	return preg_replace("/([xC0-xF7]{1,1}[x80-xBF]+)/e", '_utf8_to_html("1")', $data);
}

function _utf8_to_html($data) {
	$ret = 0;
	foreach((str_split(strrev(chr((ord($data{0}) % 252 % 248 % 240 % 224 % 192) + 128) . substr($data, 1)))) as $k => $v)
		$ret += (ord($v) % 128) * pow(64, $k);
	return "&#$ret;";
}

source

Convert special Turkish chars to international Latin chars

public static string ConvertTurkishChars(string text)
        {
            String[] olds = { "Ğ", "ğ", "Ü", "ü", "Ş", "ş", "İ", "ı", "Ö", "ö", "Ç", "ç" };
            String[] news = { "G", "g", "U", "u", "S", "s", "I", "i", "O", "o", "C", "c" };

            for (int i = 0; i < olds.Length; i++)
            {
                text = text.Replace(olds[i], news[i]);
            }

            text = text.ToUpper();

            return text;
        }

source

Convert image

find . -name "*.jpg" -print | while read ORIGINAL_FILE
do
  TARGET_FILE=$(echo "$ORIGINAL_FILE" | sed -e 's/.jpg$/.gif/')
  gm convert "$ORIGINAL_FILE" "$TARGET_FILE"
done

source

Function to convert dates to and from Mysql

<?php

/**
* function dateconvert
*
* dateconvert is a handy function to take the aches and pains out of mysqls stupidity
* by converting data from a variable (posted from a form or just stored)
* into a format mysql will be able to store and converting the
* database date back into the british standard of date month year.
* The Script accepts day.month.year or day/month/year or day-month-year.
* example:
*
* <code>
* <?php // using type 1
* $date = "19.12.2005";
* $date = dateconvert($date, 1);
* echo $date; // Would echo 2005-12-19 which is the format stored by mysql
* ?>
* </code>
* <code>
* <?php // using type 2
* $date = $row['date']; //your mysql date
* $realdate = dateconvert($date,2);
* echo $realdate; // would display 19/12/2005
* ?>
* </code>
*
* @author Chris McKee <<a href="mailto:pcdevils@gmail.com">pcdevils@gmail.com</a>>
*
* @param string $date - Date to be converted
* @param string $func - which function is to be used (1 for input to mysql, 2 for output from mysql)
*/
function dateconvert($date,$func) {
if ($func == 1){ //insert conversion
list($day, $month, $year) = split('[/.-]', $date);
$date = "$year-$month-$day";
return $date;
}
if ($func == 2){ //output conversion
list($year, $month, $day) = split('[-.]', $date);
$date = "$day/$month/$year";
return $date;
}

}

?>

source

AS3 Convert an Integer to Binary Value

var num:int = 240;
trace(num.toString(2));

// OUTPUT: 11110000

source

Convert Array to Object

// Converts an array to an object
function array2obj($data) 
{
    return is_array($data) ? (object) array_map(__FUNCTION__,$data) : $data;
}

source

Convert text to link

<?php
  function convertToURL($text) {
    $text = preg_replace("/([a-zA-Z]+<a href="://[a-z0-9_.-]+"."[a-z]{2,6}[a-zA-Z0-9/*-_?&%=,+.]+)/","" >://[a-z0-9_.-]+"."[a-z]{2,6}[a-zA-Z0-9/*-_?&%=,+.]+)/","</a> <a href="$1" target="_blank">$1</a>", $text);
    $text = preg_replace("/[^a-z]+[^<a href="://](www."."[^.]+[w][.|/][a-zA-Z0-9/*-_?&%=,+.]+)/","" >://](www."."[^.]+[w][.|/][a-zA-Z0-9/*-_?&%=,+.]+)/","</a> <a href="" target="">$1</a>", $text);
    $text = preg_replace("/([s|,>])([a-zA-Z][a-zA-Z0-9_.-]*[a-z" . "A-Z]*@[a-zA-Z][a-zA-Z0-9_.-]*[a-zA-Z]{2,6})" . "([A-Za-z0-9!?@#$%^&*()_-=+]*)" . "([s|.|,<])/i", "$1<a href="mailto:$2$3">$2</a>$4", $text);
		
    return $text;
  }
?>

source

Convert object to byte array

public static byte[] convertToByteArray(Object obj) throws IOException
  {
    ObjectOutputStream os = null;

    ByteArrayOutputStream byteStream = new ByteArrayOutputStream(5000);
    os = new ObjectOutputStream(new BufferedOutputStream(byteStream));
    os.flush();
    os.writeObject(obj);
    os.flush();
    byte[] sendBuf = byteStream.toByteArray();
    os.close();
    return sendBuf;
  }

source