예제 #1
0
 public function testConvertFromString()
 {
     $string = chr(255) . chr(254) . chr(253);
     $bitString = BitString::fromString($string);
     $this->assertEquals(24, $bitString->getLength());
     $this->assertEquals('111111111111111011111101', $bitString->toBinaryString());
     // BitString should convert back to the original string.
     $string2 = $bitString->__toString();
     $this->assertEquals($string, $string2);
 }
예제 #2
0
 /**
  * The data is treated as big-endian.  The first (left-most) character of the string
  * argument is converted into the most significant bits of the resultant bit string.
  *
  * @param string $string
  * @return BitString
  */
 public static function fromString($string)
 {
     $bitCount = strlen($string) * 8;
     $bitString = new BitString($bitCount);
     $index = $bitCount - 1;
     foreach (str_split($string) as $char) {
         $bitString->setBitsHighToLow($index, str_pad(decbin(ord($char)), 8, '0', STR_PAD_LEFT));
         $index -= 8;
     }
     return $bitString;
 }