Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将二进制字节转换为字符串_Php_Byte_Zlib_Unpack_Compression - Fatal编程技术网

Php 将二进制字节转换为字符串

Php 将二进制字节转换为字符串,php,byte,zlib,unpack,compression,Php,Byte,Zlib,Unpack,Compression,我想做的是: 1.解压缩压缩文件。 2.将未压缩的文件字节插入数组。 3.将每个字节转换为chr。 守则: <?php $list = file_get_contents('http://www.getlist.xp3.biz/list3'); //get the contents of the compressed file $list = gzuncompress($list); //Uncompress the file $bytes = unpack('c*', $list); /

我想做的是: 1.解压缩压缩文件。
2.将未压缩的文件字节插入数组。
3.将每个字节转换为chr。

守则:

<?php
$list = file_get_contents('http://www.getlist.xp3.biz/list3'); //get the contents of the compressed file
$list = gzuncompress($list); //Uncompress the file
$bytes = unpack('c*', $list); //unpack the bytes of list into array
$string = implode('', array_map('chr', $bytes)); //convert each byte to chr by implode and array_map functions
echo $string; //print results
?>
在这行的末尾有一些有线字符:
ar�7吨��Bk

此字符不是文本字节,有人(其他开发人员)将转换字节,并且他的php文件返回包含更多信息的行:

{"ver":"26","id":"12ee397313ba2dd4f27fc1430744e615e4f83a44f9b206fb78fdf9b45dd9dc74f","name":"profanador88's Room","players":"1","max_players":"10","password":false,"country":"ar","latitude":"41.0186004639","longitude":"28.9647006989","distance":1168.7633}
我有idroomname国家代码(以及werid字符(=其他信息))。

在他的行中:版本,id名称,玩家编号,最大玩家编号,如果设置了密码(布尔值),控件代码,纬度,经度和距离。

正好7更多

也许
ar�7吨��Bk
是否包含其他信息?
我认为还有其他信息(100%),我只是不知道如何转换(werid字符的)字节。
我试图询问其他开发人员关于werid chars的问题,但没有回答。
也许有人知道如何将这个二进制文件转换成普通字符串,
也许这些信息会有所帮助(另一位开发人员的注释):


我使用
chr
函数将字节转换成大字符串,hi表示字节不仅仅是字符串,
还有表示浮点、布尔、整数、短的字节。
chr是指仅表示字符串的字节(我使用它:
$string=infrade(“”,array_-map('chr',$bytes));
),这里有一个工作可能我需要在不使用此快捷方式的情况下逐字节读取每个字节。
我的问题是:
有人知道如何修复代码并使其工作(让php打印缺少的其他信息(信息在那里,但以字节(不是字符串))表示)吗?
更新
我想我找到了将字节转换为short/boolean/float/integer的类,
现在我需要有人向我解释如何使用这个类(
require('class.php')…这里的代码
),或者有人会尝试用这个类修复我的代码(我试图单独修复它,但我仍然不知道如何使用这个库,或者当我需要使用这个库时,)。
班级:


更新2:
类的版本2,如果第一个不起作用


有人知道答案是什么吗?
{"ver":"26","id":"12ee397313ba2dd4f27fc1430744e615e4f83a44f9b206fb78fdf9b45dd9dc74f","name":"profanador88's Room","players":"1","max_players":"10","password":false,"country":"ar","latitude":"41.0186004639","longitude":"28.9647006989","distance":1168.7633}
the list is a compressed binary file
which you can obtain @ /list3
it is compressed with zlib deflate
after decompression the format is as follows: (wait a bit must open the source file)
1 byte ; list format version ( currently = 1 )
4 bytes ; skip this, you don't care about them
then follows a list of RoomInfo structures until the end of the file
this is the Roominfo structure:
data.writeShort(ver); //Vrsion Short
data.writeUTF(netID); //ID String
data.writeUTF(name); //ROOMNAME string
data.writeByte(players); //PLAYERS
data.writeByte(maxPlayers); //MaxPlayers
data.writeBoolean(password); //If isset password boolean
data.writeUTF(countryCode); //country code
data.writeFloat(latitude); //Float latitude
data.writeFloat(longitude); //Float longitude
that's all
class ByteArray{

private $BytesString;

function ByteArray($bytes = "") {
        $this->BytesString = $bytes;
}

function writeBoolean($value = 1) {
        $this->BytesString .= $this->writeByte($value, False);
}

function writeByte($value, $noReturning=True) {
        if ($noReturning) $this->BytesString .= pack("C", $value);
        else return pack("C", $value);
}

function writeBytes($value) {
        $this->BytesString .= $value;
}

function writeInt($value) {
        $this->BytesString .= pack('N', $value);
}

function writeShort($value) {
        $this->BytesString .= pack('n', $value);
}

function writeUTF($value) {
        $valueSize = strlen($value);
        $this->writeShort($valueSize);
        $this->writeUTFBytes($value);
}

function writeUTFBytes($value) {
        $this->BytesString .= $value;
}

function length() {
        return strlen($this->BytesString);
}

function toString() {
        return $this->BytesString;
}

function toPack() {
        $value = pack('N', strlen($this->BytesString)+4);
        return $value.$this->BytesString;
}

function getSize() {
        $value = unpack('N', substr($this->BytesString, 0, 4));
        return $value[1];
}

function readBy($Pos) {
        $this->BytesString = substr($this->BytesString, $Pos);
        return $this->BytesString;
}

function loc($byte) {
        $loc = substr($this->BytesString, 0, $byte);
        $this->BytesString = substr($this->BytesString, $byte);
        return unpack('C', $loc);
}

function readInt() {
        $size = unpack('N', substr($this->BytesString, 0, 4)); $size = $size[1];
        $this->BytesString = substr($this->BytesString, 4);
        return $size;
}

function readUTF() {
        $size = unpack('n', substr($this->BytesString, 0, 2)); $size = $size[1];
        $string = substr($this->BytesString, 2, $size);
        $this->BytesString = substr($this->BytesString, $size + 2);
        return $string;
}

function readShort() {
        $size = unpack('n', substr($this->BytesString, 0, 2)); $size = $size[1];
        $this->BytesString = substr($this->BytesString, 2);
        return $size;
}

function readBoolean() {
        $loc = unpack('C', substr($this->BytesString, 0, 1)); $loc = $loc[1];
        $this->BytesString = substr($this->BytesString, 1);
        if ($loc == 1) return True;
        else return False;
}

function readByte() {
        $byte = unpack('C', substr($this->BytesString, 0, 1)); $byte = $byte[1];
        $this->BytesString = substr($this->BytesString, 1);
        return $byte;
}

}
<?php
class ByteArray {
protected $bigEndian = TRUE;
protected $byteArray;
protected $capacity;
protected $limit;
protected $mark;
public $position;
public function __construct($byteArray = '') {
$this->byteArray = $byteArray;
$this->position = 0;
$this->mark = - 1;
$this->init ();
}
private function init() {
$this->capacity = strlen ( $this->byteArray );
$this->limit = $this->capacity;
}
public function _array() {
return $this->byteArray;
}
public function clear() {
$this->limit = $this->capacity;
$this->position = 0;
$this->mark = - 1;
}
private function get($length = null) {
if ($length === null) {
$length = $this->limit - $this->position;
} elseif ($length > $this->bytesAvailable ()) {
throw new Exception ( 'bytesAvailable' );
}
$data = substr ( $this->byteArray, $this->position, $length );
$this->position += $length;
return $data;
}
private function set($bytes) {
$p1 = substr ( $this->byteArray, 0, $this->position );
$p2 = substr ( $this->byteArray, $this->position );
$len = strlen ( $bytes );
if ($len < strlen ( $p2 )) {
$p2 = substr ( $p2, $len );
} else {
$p2 = '';
}
$p1 .= $bytes . $p2;
$this->byteArray = $p1;
$this->position += $len;
$this->init ();
}
public function readBytes($length = -1, $offset = -1) {
$limit = $this->limit;
if ($offset == - 1) {
$offset = $this->position;
}
if ($length == - 1) {
$length = $limit - $offset;
}
if ($length > $limit - $offset) {
return null;
}
return substr ( $this->byteArray, $offset, $length );
}
public function writeBytes($bytes, $offset = 0, $length = 0) {
$len = strlen ( $bytes );
if ($len < 1) {
return;
}
if ($length < 1) {
$length = $len;
}
if ($offset < 1) {
$offset = 0;
}
if ($offset + $length > $len) {
return;
}
$p1 = substr ( $bytes, $offset, $length );
$this->set ( $p1 );
}
public function readBoolean() {
return $this->readByte () != 0;
}
public function writeBoolean($value) {
$this->writeByte ( $value != 0 );
}
public function readByte() {
return ord ( $this->get ( 1 ) );
}
public function readUnsignedByte() {
$data = unpack ( 'C', $this->get ( 1 ) );
return $data [1];
}
public function writeByte($value) {
$data = pack ( 'c', $value );
$this->set ( $data );
}
public function readShort() {
$data = unpack ( $this->bigEndian ? 'n' : 'v', $this->get ( 2 ) );
return $data [1];
}
public function writeShort($value) {
$data = pack ( $this->bigEndian ? 'n' : 'v', $value );
$this->set ( $data );
}
public function readInt() {
$data = unpack ( $this->bigEndian ? 'N' : 'V', $this->get ( 4 ) );
return $data [1];
}
public function writeInt($value) {
$data = pack ( $this->bigEndian ? 'N' : 'V', $value );
$this->set ( $data );
}
public function readFloat() {
$data = unpack ( 'f', $this->get ( 4 ) );
return $data [1];
}
public function writeFloat($value) {
$data = pack ( 'f', $value );
$this->set ( $data );
}
public function readDouble() {
$data = unpack ( 'd', $this->get ( 8 ) );
return $data [1];
}
public function writeDouble($value) {
$data = pack ( 'd', $value );
$this->set ( $data );
}
public function readString() {
$length = $this->readShort ();
$value = $this->get ( $length );
return $value;
}
public function writeString($value) {
$len = strlen ( $value );
$this->writeShort ( $len );
$this->writeStringBytes ( $value );
}
public function writeStringBytes($value) {
$len = strlen ( $value );
$data = pack ( 'a' . $len, $value );
$this->set ( $data );
}
public function readStringBytes($length) {
return $this->get ( $length );
}
public function bytesAvailable() {
return $this->limit - $this->position;
}
public function length() {
return $this->limit;
}
public function __toString() {
return $this->byteArray;
}
public function compress($level = 5) {
$this->byteArray = gzcompress ( $this->byteArray, $level );
$this->init ();
}
public function uncompress($level = 5) {
$this->byteArray = gzuncompress ( $this->byteArray, $level );
$this->init ();
}
}

?>