Solved

Convert 7bit text into octets (8bit)

Posted on 2004-08-18
7
7,979 Views
Last Modified: 2011-10-03
Hi,

I have been looking for a way to do this in php for a while and cant automate this....

I am looking for a php function that can take in normal 7bit text, like "hellohello" which is 10 characters and convert them to 8bit octets (ie. E8 32 9B FD 46 97 D9 EC 37). If you look at:

http://www.dreamfabric.com/sms/hello.html

Quote from the page:
-----------------------------
The first septet (h) is turned into an octet by adding the rightmost bit of the second septet. This bit is inserted to the left which yields 1 + 1101000 = 11101000 ("E8"). The rightmost bit of the second character is then consumed, so the second character (septet) needs two bits (yellow) of the third character to make an 8bit octet. This process goes on and on yielding the following octets:

The 9 octets from "hellohello" are E8 32 9B FD 46 97 D9 EC 37
-----------------------------

Thanks in advance!
Bill.
0
Comment
Question by:billbrigden
  • 2
  • 2
7 Comments
 

Author Comment

by:billbrigden
ID: 11829058
Further to my question - if anyone can do the reverse as well, ie. The octets, E8 32 9B FD 46 97 D9 EC 37 back into "hellohello" that would be great.

Cheers.
Bill.
0
 
LVL 6

Accepted Solution

by:
CosminB earned 500 total points
ID: 11830205
Try this:
<?
function seven2eight($string, $param = 'hexa')
{
      $l = strlen($string);
      $a = array();
      for ($i=0;$i<$l; $i++)
      {
            $a[] = decbin(ord($string{$i})); //convert to bin
      }
      $step = 0;
      for ($i=0; $i<$l-1; $i++)
      {
            $step = (++$step)%8;
            $toAdd =  strrev(substr(strrev($a[$i+1]), 0, $step));
            $a[$i] = substr($toAdd.$a[$i], 0, 8);
      }
      //strip the extra values
      for ($i=0; $i<$l;$i++)
      {
            if (($i+1)%8 != 0)
                  $resultArray[] = $a[$i];
      }
      
      
      if ($param == 'array')
      {
            return $resultArray;
      }
      if ($param == 'hexa')
      {
            for ($i=0;$i<count($resultArray); $i++)
            {
                  $resultArray[$i] = dechex(bindec($resultArray[$i]));
            }
            return $resultArray;
      }
      if ($param == 'string')
      {
            $result = '';
            
            for ($i=0;$i<count($resultArray); $i++)
            {
                  $result.= chr(bindec($resultArray[$i]));
            }
            return $result;
      }
}

//possible value for the seconf parameter are 'array', 'hexa' and 'string'

print_r(seven2eight($string, 'array'));
print_r(seven2eight($string, 'hexa'));
print_r(seven2eight($string, 'string'));
?>

the code is a little messy but it will work. the reverse is very similar to this but I don't have time to do it right now ... maybe tomorrow
0
 
LVL 6

Expert Comment

by:CosminB
ID: 11830214
... of course you have to initialise $string with 'hellohello' or smth. before calling seven2eight()
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 11831673
so far this is what I have.

<?php

function convert7to8($str) {
    $newarr=array();
    $bins=array();
    $let='';
    for ($i=0;$i<strlen($str);$i++){
        $let = substr($str,$i,1);
        $bins[] = decbin(ord($let));
    }

    $carry='';
    for($i=0;$i<count($bins)-1;$i++) {
        $cnt = ($i+1) % 8;
        if ($bins[$i] != '') {
            $carry = substr($bins[$i+1],-1 * $cnt);
            if ($carry) {
                $bins[$i+1] = substr($bins[$i+1],0,-1 * $cnt);
            }
            $bins[$i] = $carry . $bins[$i];
            $newarr[] = dechex(bindec($bins[$i]));
        }
    }
    if ($bins[$i] != '') {
        $newarr[] = dechex(bindec($bins[$i]));
    }
    return $newarr;
}

// expecting array of hex characters
function convert8to7($arr) {
    if (!is_array($arr)) {
        echo "expecting array of hex characters\n";
    } else {
        $carry='';
        $curBin='';
        $oldCarry='';
        $newArr=array();
        $cnt=0;
        for($i=0;$i<count($arr)-1;$i++) {
            $cnt = (++$cnt) % 8;
            $curBin = decbin(hexdec($arr[$i]));
            while (strlen($curBin) < 8) { $curBin = '0'.$curBin; }
            $carry = substr($curBin, 0, $cnt);
            $curBin = substr($curBin, $cnt);
            $newArr[] = $curBin.$oldCarry;
            if ((($cnt+1) % 8) == 0) {
                $newArr[] = $carry;
                $carry='';
                $cnt = (++$cnt) % 8;
            }
            $oldCarry=$carry;
        }
        $newArr[] = decbin(hexdec($arr[$i])).$oldCarry;

        $newStr = '';
        foreach($newArr as $bin){
            $newStr .= chr(bindec($bin));
        }
        return $newStr;
    }
}

$newstr = convert7to8("hellohello");
echo "The " . count($newstr) . " octets are " . strtoupper(join(' ', $newstr)) . "<br/>\n";
print_r(convert8to7($newstr));

?>

The convert8to7 I have works for some, but for some reason it does not work for anything ending in p
ex. hellohelp
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 11833646
or rather ... it doesn't work with 'hellohellp' or 'hellohellohellop' (this chops the p off completely)

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
change password links 7 72
How can i modify my File download link ? 6 47
Amazon Redshift 2 25
using php variable inside javascript 5 13
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now