Page 1 of 1

Convert String to Hex

Posted: Fri Mar 25, 2011 3:51 pm
by alm2009
Hi, i need to represnt "string" that it is the actual Hexadecimal value as Hexadecimal.
I have data that I am receiving within text file.
It has all kind of data, one of the data that I am extracting from the file is hexadecimal number which like the
( RX= 8288684E, BX= 8388684E…) But it does not have the “0x” prefix that defines the hex number.
I need to take those values and perform bit mask on them like here

Code: Select all

$mask = 0x8288684E & 0x00000100;
Since the first value I have doesn’t have the “0x” in front of the variable it fails to give me the correct result.

Tried to do this

Code: Select all

$val = “8288684E”;
$a = “0x”;
$hexVal = $a . $val;
$mask = $hexVal & 0x00000100;
Didn’t work!

Here is my compete code
Help would appriciated

Code: Select all

<?php  //RX.php

define('B00', 0x00000001);
define('B01', 0x00000002);
define('B05', 0x00000010);
define('B08', 0x00000100);
define('B09', 0x00000200);
define('B10', 0x00000400);
define('B11', 0x00000800);
define('B12', 0x00001000);
define('B13', 0x00002000);
define('B14', 0x00004000);
define('B15', 0x00008000);
define('B16', 0x00010000);
define('B17', 0x00020000);
define('B18', 0x00040000);
define('B19', 0x00080000);
define('B20', 0x00100000);
define('B21', 0x00200000);
define('B22', 0x00400000);
define('B23', 0x00800000);
define('B24', 0x01000000);
define('B25', 0x02000000);
define('B27', 0x08000000);
define('B29', 0x20000000);
define('B30', 0x40000000);
define('B31', 0x80000000);	
   
function getRx($RX) {
 	$Bit00 = ($RX & B00) ? TRUE:FALSE;
    $Bit01 = ($RX & B01) ? TRUE:FALSE;
	$Bit05 = ($RX & B05) ? TRUE:FALSE;
	$Bit08 = ($RX & B08) ? TRUE:FALSE;
	$Bit09 = ($RX & B09) ? TRUE:FALSE;
	$Bit10 = ($RX & B10) ? TRUE:FALSE; 
	$Bit11 = ($RX & B11) ? TRUE:FALSE;
	$Bit12 = ($RX & B12) ? TRUE:FALSE;
	$Bit13 = ($RX & B13) ? TRUE:FALSE;
	$Bit14 = ($RX & B14) ? TRUE:FALSE;
	$Bit15 = ($RX & B15) ? TRUE:FALSE;
	$Bit16 = ($RX & B16) ? TRUE:FALSE;
	$Bit17 = ($RX & B17) ? TRUE:FALSE;
	$Bit18 = ($RX & B18) ? TRUE:FALSE;
	$Bit19 = ($RX & B19) ? TRUE:FALSE;
	$Bit20 = ($RX & B20) ? TRUE:FALSE;
	$Bit21 = ($RX & B21) ? TRUE:FALSE;
	$Bit22 = ($RX & B22) ? TRUE:FALSE;
	$Bit23 = ($RX & B23) ? TRUE:FALSE;
	$Bit24 = ($RX & B24) ? TRUE:FALSE;
	$Bit25 = ($RX & B25) ? TRUE:FALSE;
	$Bit27 = ($RX & B27) ? TRUE:FALSE;
	$Bit29 = ($RX & B29) ? TRUE:FALSE;
	$Bit30 = ($RX & B39) ? TRUE:FALSE;
	$Bit31 = ($RX & B31) ? TRUE:FALSE;
    
	if ($Bit5) return "Pres";
	else if (!$Bit25) return "Pumps";
	else if ($Bit28) return "Sync";
	else if ($Bit29 & !$Bit30) return "S";
	else if ($Bit29 & $Bit30) return "Missed";
	else if (!$Bit29 & $Bit30)return "Mis";
	else if ($Bit31 & $Bit17) return "Rec";
	else if ($Bit31 & $Bit21) return "Rece";
	else if ($Bit31 & $Bit09) return "Recei";
	else if ($Bit31 & $Bit13) return "Receiv";
	else if ($Bit31 & $Bit12) return "No";
}
 
?>

Re: Convert String to Hex

Posted: Fri Mar 25, 2011 5:05 pm
by Weirdan

Code: Select all

$val = "0288684E";
sscanf($val, "%X", $hexVal);
var_dump($hexVal, 0x0288684e);
Since php int is signed, on 32bit systems it might give you wrong results for resulting values where the most significant bit would be set. Shouldn't be a problem on 64bit PHP though (check PHP_INT_SIZE constant).

Re: Convert String to Hex

Posted: Fri Mar 25, 2011 5:37 pm
by Apollo
Use the hexdec function.

Re: Convert String to Hex

Posted: Fri Mar 25, 2011 9:02 pm
by afr_dnf2011
You may also use the function below.

Code: Select all

function strTohex($str_text)
{
    $hex_num='';
    for ($i=0; $i < strlen($str_text); $i++)
    {
        $hex_num .= dechex(ord($str_text[$i]));
    }
    return $hex_num;
}

Re: Convert String to Hex

Posted: Mon Mar 28, 2011 3:43 am
by alm2009
Thank all

i used Apollo's suggestion

thank you.