Parsing a binary file

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
kepler
New php-forum User
New php-forum User
Posts: 7
Joined: Sun Jul 16, 2017 9:38 pm

Fri May 11, 2018 3:00 pm

Good evening,

I need some help. I have made a binary file with a sequence of single precision numbers. Can someone help me write a small code (an example) where I can open the files (in binary mode of course) and read a block of 4 bytes at position n and convert it back to a single precision number?

I apreciate the help. My problem is somewhat the confusion with ftell and fseek, etc. :(

Kind regards,

Kepler

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 720
Joined: Mon Feb 22, 2016 5:52 pm

Sat May 12, 2018 2:32 am

fread can open binary files, then I think you are looking for unpack to translate the data and the pack page will give you the data type codes.

PHP tries to avoid binary data since it is largely machine dependant and portability is the goal. Using binary formats could lead to problems at a later date for instance if 32 bit support were dropped.

IMO JSON would be the format of choice when transferring data from one source to another.

Post Reply