public function testConvertToStringMultiWord() { $bitString = new BitString(64); $bitString->setBitsHighToLow(63, '1111111111111111111111111111111100000000000000000000000000000000'); $string = $bitString->__toString(); // 64-bits should map to 8 8-bit characters. $this->assertEquals(8, strlen($string)); $chars = str_split($string); $this->assertEquals(chr(255), $chars[0]); $this->assertEquals(chr(0), $chars[count($chars) - 1]); $bitString2 = BitString::fromString($string); $this->assertEquals('1111111111111111111111111111111100000000000000000000000000000000', $bitString2->toBinaryString()); }
/** * 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; }