简体   繁体   中英

Reading binary file in php and converting it into string

I spent almost a day for this , but did not get success.

What i want to do is, i have a binary file "data.dat"

I want to read the file contents and output it in text format in say "data.txt" in php.

I tried unpack function of php, but requires the type to be mentioned as the first argument(May be i am wrong, new to php).

$data = fread($file, 4); // 4 is the byte size of a whole on a 32-bit PC.     
$content= unpack("C", $data);    //C for unsigned charecter , i for int and so on...

But what if i dont know that at what place , what type of data is stored in the file that i am reading?

This function is restricting me because of the type.

I want something similar to this

$content= unpack("s", $data);   //where s can denote to string

Thanks.

PHP does not have a "binary" type. Binary data is stored in strings. If you read binary data from a file, it's already stored as a string. You do not need to convert it into a string.

If the binary data already represents text in some standard encoding, you don't need to do anything as you already have a valid string. If the binary data represents some encoding, you need to know what you need to do with it, we don't know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM