Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
[email protected] web/email now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!

Paste

Pasted as PHP by registered user electronico_nc ( 12 years ago )
function strToHex($string)
{
    $hex='';
    for ($i=0; $i < strlen($string); $i++)
    {
        $hex .= dechex(ord($string[$i]));
    }
    return $hex;
}

function hexToStr($hex)
{
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2)
    {
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}

function ucs2ToStr($hex)
{
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2)
    {
   if ( $hex[$i].$hex[$i+1] == "00" ) { 
    $i+=2;
   }
   $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}

function hexbin($hex){
    $bin='';
    for($i=0;$i<strlen($hex);$i++)
        $bin.=str_pad(decbin(hexdec($hex{$i})),4,'0',STR_PAD_LEFT);
       return $bin;
} 

function binhex($bin){
    $hex='';
    for($i=strlen($bin)-4;$i>=0;$i-=4)
        $hex.=dechex(bindec(substr($bin,$i,4)));
   return strrev($hex);
}

function ucs2toutf8($str)
{
        for ($i=0;$i<strlen($str);$i+=4)
        {
                $substring1 = $str[$i].$str[$i+1];
                $substring2 = $str[$i+2].$str[$i+3];
 
                if ($substring1 == "00")
                {
                        $byte1 = "";
                        $byte2 = $substring2;
                }
                else
                {
                        $substring = $substring1.$substring2;
                        $byte1 = dechex(192+(hexdec($substring)/64));
                        $byte2 = dechex(128+(hexdec($substring)d));
                }
                $utf8 .= $byte1.$byte2;
        }
        return $utf8;
} 

 function ucs2html($str) {
    $str=trim($str); // if you are reading from file
    $len=strlen($str);
    $html='';
    for($i=0;$i<$len;$i+=2)
        $html.='&#'.hexdec(dechex(ord($str[$i+1])).
                   sprintf("s",dechex(ord($str[$i])))).';';
    return($html);
 }


function loadgsm7arrs()
{

  global $g2aarr, $a2garr;

  $g2aarr = array(
                  0x0040,
                  0x00A3,
                  0x0024,
                  0x00A5,
                  0x00E8,
                  0x00E9,
                  0x00F9,
                  0x00EC,
                  0x00F2,
                  0x00E7,
                  0x000A,
                  0x00D8,
                  0x00F8,
                  0x000D,
                  0x00C5,
                  0x00E5,
                  0x0394,
                  0x005F,
                  0x03A6,
                  0x0393,
                  0x039B,
                  0x03A9,
                  0x03A0,
                  0x03A8,
                  0x03A3,
                  0x0398,
                  0x039E,
                  0x00A0,
                  0x00C6,
                  0x00E6,
                  0x00DF,
                  0x00C9,
                  0x0020,
                  0x0021,
                  0x0022,
                  0x0023,
                  0x00A4,
                  0x0025,
                  0x0026,
                  0x0027,
                  0x0028,
                  0x0029,
                  0x002A,
                  0x002B,
                  0x002C,
                  0x002D,
                  0x002E,
                  0x002F,
                  0x0030,
                  0x0031,
                  0x0032,
                  0x0033,
                  0x0034,
                  0x0035,
                  0x0036,
                  0x0037,
                  0x0038,
                  0x0039,
                  0x003A,
                  0x003B,
                  0x003C,
                  0x003D,
                  0x003E,
                  0x003F,
                  0x00A1,
                  0x0041,
                  0x0042,
                  0x0043,
                  0x0044,
                  0x0045,
                  0x0046,
                  0x0047,
                  0x0048,
                  0x0049,
                  0x004A,
                  0x004B,
                  0x004C,
                  0x004D,
                  0x004E,
                  0x004F,
                  0x0050,
                  0x0051,
                  0x0052,
                  0x0053,
                  0x0054,
                  0x0055,
                  0x0056,
                  0x0057,
                  0x0058,
                  0x0059,
                  0x005A,
                  0x00C4,
                  0x00D6,
                  0x00D1,
                  0x00DC,
                  0x00A7,
                  0x00BF,
                  0x0061,
                  0x0062,
                  0x0063,
                  0x0064,
                  0x0065,
                  0x0066,
                  0x0067,
                  0x0068,
                  0x0069,
                  0x006A,
                  0x006B,
                  0x006C,
                  0x006D,
                  0x006E,
                  0x006F,
                  0x0070,
                  0x0071,
                  0x0072,
                  0x0073,
                  0x0074,
                  0x0075,
                  0x0076,
                  0x0077,
                  0x0078,
                  0x0079,
                  0x007A,
                  0x00E4,
                  0x00F6,
                  0x00F1,
                  0x00FC,
                  0x00E0 
                        );

  // create $a2garr
  // just flip the contents of keys and values
  foreach($g2aarr as $key => $val)
  {
    $a2garr[$val] = $key;
  }

  // then add the special chars we want to translate our way
  // in some cases they will be overwritten, for a reason
  $a2garr[13]   =  10;
  $a2garr[126]  =  32;
  $a2garr[94]   =  32;
  $a2garr[123]  =  40;
  $a2garr[125]  =  41;
  $a2garr[92]   =  47;
  $a2garr[91]   =  40;
  $a2garr[93]   =  41;
  $a2garr[124]  = 161;
  $a2garr[96]   =  39;
  $a2garr[8364] = 101;

}

//// ----------------------------------------
// asc2pdu()
//
// translates a string from regular alphabet to gsm-7
// encodes a string from septets to octets
// based upon 3GPP TS 23.038
// provides padding to match the septet boundary
//
// required parameters:
// - text
//   can be any length
// - padding bits
//   depending upon how long the header is from UDHL on,
//   we might need to add some padding bits as the pdu message
//   starts counting in septets from UDHL (inclusive),
//   but the whole UDH is in octets; padding is necessary
//   to start on the right septet boundary.
//   for further reference, check:
//   mobiletidings.com/2009/02/18/combining-sms-messages and
//   www.dreamfabric.com/sms
//
// needed globals:
// $a2garr[]
//
// returns {string}
// - translated/encoded string
//

function asc2pdu($txt, $pad)
{

  // global declare
  global $a2garr;

  // create a binary string starting with the
  // requested amount of padding bits (set to zero)
  $bstr = str_repeat('0', $pad);

  // sweep $txt for each character
  // append the 7 least significant bits to $bstr
  $txtlen = strlen($txt);
  for($tind = 0; $tind < $txtlen; $tind++)
  {

    // extract the char
    $xchr = substr($txt, $tind, 1);

    // read the ascii decimal chr
    $achr = ord($xchr);

    // translate it into gsm-7
    $tchr = $a2garr[$achr];

    // get the binary
    $bchr = base_convert($tchr, 10, 2);

    // pad it with zeroes
    $pchr = str_pad($bchr, 8, '0', STR_PAD_LEFT);

    // if it's in the high half of the byte
    // - log an error -- class 1
    // - change it into space
    //
    // further investigation incoming
    //
    if($pchr > 10000000)
    {
      // find your way to log the error
      // echoing is not the brightest way to do it :)
      echo "Found a char in the high byte half for text: ".$txt.
               " -- char is asc".$tchr."\r\n";
      $phcr = str_pad(base_convert('20', 16, 2), 8, '0', STR_PAD_LEFT);
    }

    // strip off the highest bit
    $schr = substr($pchr, 1);

    // reverse it and add to the resulting string
    $bstr .= strrev($schr);

  }

  // split into 8-bit chunks
  $carr = str_split($bstr, 8);

  // put together the hex string
  $hstr = '';
  foreach($carr as $chunk)
  {
    // reverse
    $rchk = strrev($chunk);

    // convert into hex
    $hchk = base_convert($rchk, 2, 16);

    // left pad with zeroes
    // add it to the hex string
    $hstr .= str_pad($hchk, 2, '0', STR_PAD_LEFT);
  }

  return $hstr;

}


//// ----------------------------------------
// pdu2asc()
//
// translates a string from gsm-7 to regular alphabet
// decodes a string from octets to septets
// based upon 3GPP TS 23.038
// provides psdding bits stripping
//
// required parameters:
// - text
// - padding bits to strip
//
// see header of asc2pdu()
//
// needed globals:
// $g2aarr[]
//
// returns {string}
// - translated/decoded string
//

function pdu2asc($pdu, $pad)
{

  // global declare
  global $g2aarr;

  // converting all octets in a big binary string
  $pdulen = strlen($pdu);

  $bstr = '';
  for($lind = 0; $lind < $pdulen; $lind += 2)
  {
    // extract the octet
    $xoct = substr($pdu, $lind, 2);

    // translate in binary
    $boct = base_convert($xoct, 16, 2);

    // pad it for the non significant zeroes
    $poct = str_pad($boct, 8, '0', STR_PAD_LEFT);

    // reverse it
    // add it to the binary string
    $bstr .= strrev($poct);
  }

  // strip off the padding bits
  // it has been previously added from the sender
  // to fit the next septet boundary
  // and we know exactly how many they are supposed to be
  $bstr = substr($bstr, $pad);

  // kill the extra bits off the last unfinished chunk, if any
  // or they would translate into nice @s
  $bmod = strlen($bstr) % 7;
  if($bmod)
  {
    $bstr = substr($bstr, 0, -$bmod);
  }

  // split it into 7-bit chunks
  $carr = str_split($bstr, 7);

  // put together the ascii string
  $astr = '';
  foreach($carr as $chunk)
  {
    // pad it with the non significant bits
    $pchk = str_pad(strrev($chunk), 8, '0', STR_PAD_LEFT);

    // translate it into ascii decimal
    $dchk = base_convert($pchk, 2, 10);

    // translate it into regular ascii alphabet
    $achk = $g2aarr[$dchk];

    // make it an ascii char and add it to the ascii string
    $astr .= chr($achk);
  }

  return $astr;

}

function utf16_to_utf8($str) {
    $c0 = ord($str[0]);
    $c1 = ord($str[1]);

    if ($c0 == 0xFE && $c1 == 0xFF) {
        $be = true;
    } else if ($c0 == 0xFF && $c1 == 0xFE) {
        $be = false;
    } else {
        return $str;
    }

    $str = substr($str, 2);
    $len = strlen($str);
    $dec = '';
    for ($i = 0; $i < $len; $i += 2) {
        $c = ($be) ? ord($str[$i]) << 8 | ord($str[$i + 1]) : 
                ord($str[$i + 1]) << 8 | ord($str[$i]);
        if ($c >= 0x0001 && $c <= 0x007F) {
            $dec .= chr($c);
        } else if ($c > 0x07FF) {
            $dec .= chr(0xE0 | (($c >> 12) & 0x0F));
            $dec .= chr(0x80 | (($c >>  6) & 0x3F));
            $dec .= chr(0x80 | (($c >>  0) & 0x3F));
        } else {
            $dec .= chr(0xC0 | (($c >>  6) & 0x1F));
            $dec .= chr(0x80 | (($c >>  0) & 0x3F));
        }
    }
    return $dec;
}

 

Revise this Paste

Your Name: Code Language: