Java hex2text convert

Here is the code for converting hex into a normal char.
JAVA CODE:

public static String hex2Text(String str){
if(str == null || str.trim().length() == 0)
return "";
str = str.trim();
str = str.toLowerCase(); //ensures all input is lower case
if(str.matches("[^0-9a-f\\s]")){
/*
User needs to be warned that their input is invalid
Method must terminate
*/
return ""; //Until proper exception can be written
}
StringTokenizer tokenizedStr = new StringTokenizer(str);
StringBuffer text = new StringBuffer();
while(tokenizedStr.hasMoreTokens()){
String hexString = tokenizedStr.nextToken();
int bytelen = hexString.length();
if(bytelen > 4){
//User needs to be told input is not valid
return ""; //Until exception can be written.
}
int decimal = hex2decimal(hexString);
int intValue = 0;
for(int i = 0;i < strlen;++i){
int oneOrZero = decimal % 10;
decimal /= 10;
intValue += Math.pow(2,i) * oneOrZero;
}//end for
char textEquivalent = (char) intValue;
text.append(textEquivalent);
}//end while
return text.toString();
}

private static int hex2decimal(String hex){
int decimal = 0;
for(int i = hex.length() - 1;i >= 0;--i){
int asciiValue = hex.charAt(i);
if(asciiValue >= '0' && asciiValue <= '9')
asciiValue -= 48;
else{
asciiValue -= 87;
}
decimal += Math.pow(16,i) * asciiValue;
}//end for
return decimal;
}//end hex2decimal()

Convert HEX2TEXT

Converting hex into a text or normal string.
PHP CODE

function hex2text($hexstr) {
$hex = explode('%',$hexstr);
array_shift($hex);
$str = '';
foreach($hex as $hexcode) {
$str .= chr(base_convert($hexcode, 16, 10));
}
return $str;
}

PHP insert blob and update blob MySQL

Here is how to insert file BLOB MySQL.
PHP CODE:

if($_POST[submit]){
$file_name = $_FILES['file']['name'];
$tmp_name = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$fp = fopen($tmp_name, 'r');
$file_content = fread($fp, $file_size) or die("Error: cannot read file");
$file_content = mysql_real_escape_string($file_content) or die("Error: cannot read file");
fclose($fp);
// INSERT
$qu = "INSERT INTO `file`
(`file_content`,`file_name`,`file_type`,`file_size`)
VALUES
('$content','$file_name','$file_type','$file_size')";
$re = mysql_query($qu) or die ("Sorry Cant insert db!");
echo $file_name." inserted succesfully to database";
}
echo "< form method=\"post\" name=\"form1\" enctype=\"multipart/form-data\">";
echo " < input name=\"file\" type=\"file\">";
echo " < input name=\"submit\" type=\"submit\" value=\"Upload\">";
echo "< /form>";

How to Protect from double posting insert MySQL

Submit once even the visitor is refresh the page from browser.
Here is the way to protect.
Use a SESSION.
1. Set a session in the form page

session_start();
$_SESSION[post_only_once] = 1;
echo "
< form method=\"post\">
Name < input type=\"text\" name=\"name\">
< input type=\"submit\" value=\"submit\" name=\"submit\">
< /form>
";


2. Check the session before doing INSERT into MySQL.
session_start();
if($_SESSION[post_only_once] == 1) {
// here doing insert to mysql
}
unset($_SESSION[post_only_once]); // remove the session after insert

How to connect MySQL from PHP

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

PHP br2nl function

PHP CODE

How to detect browser no flash give alternate with gif

Let see, we have one file .swf and one file .gif and we want display the flash file first, and if the browser doesn't support, it will display the .gif version.
Here is the code.

JAVASCRIPT CODE:

How to pick a complex string from MySQL

We can pick a string from MySQL database with various style.
Example, the value from database is NCABZ-2007
And we want only view ABZ
Here is the CODE.
PHP CODE:

$data = "NCABZ-2007";
$new_data = substr($data, 2 , 3);

Explain:
2 = number of data from the left (N=0, C=1, A=2, B=3, etc.)
3 = how much char we want to display

PHP Cut string from MySQL

Shorting or cutting the text string from long char into new max lenght.
Here is the code.
PHP CODE

function short_string ($var, $len){    
if (strlen ($var) < $len) {
return $var;
}
if (preg_match ("/(.{1,$len})\s/", $var, $match)) {
return $match [1] . "..";
} else {
return substr ($var, 0, $len) . "..";
}
}


How to use?
PHPCODE
short_string ("Very very long text is here blablabla", 14);


The Result is:
Very very long..

Insert and Select doing in MySQL from SQL Injection

Here is the save way from RSS Attack SQL Injection.
Get all variable $_POST or $_GET or $_REQUEST or $_SESSION convert into mysql_real_escape string filter, you do not need one by one for all type variable.
here is code.
PHP CODE

$_POST  = array_map('mysql_real_escape_string', $_POST);
$_SESSION = array_map('mysql_real_escape_string', $_SESSION);
$_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);


And here is to view back or mysql_unescape_string or mysql_real_unescpae_string or whatever you want to.
PHP CODE
function mysql_real_unescape_string($string){
$string=trim($string);
$string=str_replace("\\","",str_replace("\$","",$string));
return $string;
}

Convert TEXT2HEX

Here is a php script snippet to convert string text to hex decimal code.
PHP CODE

function text2hex($string) {
$hex = '';
$len =
strlen($string) ;
for ($i = 0; $i < $len; $i++) {
$hex .=
str_pad(dechex(ord($string[$i])), 2, 0, STR_PAD_LEFT);
}
return $hex;
}