/    Sign up×
Community /Pin to ProfileBookmark

Storing Binary data as text in files

First of all, would most of you agree that unless there is a large enough amount of data to affect performance from accumulated conversion times, it usually makes sense to store binary data as text when writing and reading files, correct?. It certainly seems easier from a troubleshooting issues, because until the file(s) are removed, they are at least humanly readable. There is plenty of storage space these days, and who needs compatibility issues when a future version of PHP defines an integer as being an 8 byte number, or another program that assumes a different byte order tries to read your files.

Second, assuming i want to save as text, what is the correct way (without taking language shortcuts) to make sure binaries stored in variables (integers, floating pt. numbers, or simple true/false states) are converted and saved as text in a file, and converted back to binary when read back from the file. PHP seems very loose with its rules over data-typing compared to other languages I’ve worked with, so I’d not be surprised if I did it technically wrong but still got a correct result. Can someone give me simple examples of the correct ways?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 30.2018 — I'm not sure why it matters for file storage, as everything ends up stored a ones and zeros (a.k.a. binary). ?

All I can contribute is that PHP has bin2hex() and hex2bin() for such conversions.
Copy linkTweet thisAlerts:
@PeterPan_321authorNov 30.2018 — @NogDog#1598454 What i mean, for example, is given a variable $x = 100; how to store it in a file as "100", and make sure it gets placed in a variable as 100, and not "100" when I read it back from the file.
Copy linkTweet thisAlerts:
@PeterPan_321authorNov 30.2018 — Oh hell! I just realized there are fprintf() and fscanf() calls in PHP! Well I guess that's the answer then. At least its one that is familiar to me as an old school 'C' guy!
Copy linkTweet thisAlerts:
@rootNov 30.2018 — You are aware of the fact that binary files, especially executable files can end up damaged, appear to work but pose a potential issue, converting from a file to string of text in a file is a very, very bad idea, thats the same idea as storing an image in a database.

YOU have to remember that all these files are optimised already and what you try to do to them thereafter can break them.

I believe I have already mentioned this and if you search the WWW, you will find articles about and advising against doing it for some of the very reasons I have given you already.
Copy linkTweet thisAlerts:
@NogDogNov 30.2018 — Depending on what you are doing, you might want to store the data as JSON text in the file, then when you use json_decode() on the file contents, it will be the correct type -- assuming you stored it as such to begin with.
<i>
</i>16:14 $ php -a
Interactive shell

php &gt; $int = 200;
php &gt; $txt = '300';
php &gt; $data = array('int' =&gt; $int, 'txt' =&gt; $txt);
php &gt; var_export($data);
array (
'int' =&gt; 200,
'txt' =&gt; '300',
)
php &gt; $json = json_encode($data);
php &gt; file_put_contents('./test_data.txt', $json);
php &gt; readfile('./test_data.txt');
{"int":200,"txt":"300"}
php &gt; $read_data = json_decode(file_get_contents('./test_data.txt'), 1);
php &gt; var_export($read_data);
array (
'int' =&gt; 200,
'txt' =&gt; '300',
)
php &gt; $int = $read_data['int'];
php &gt; $txt = $read_data['txt'];
php &gt; var_export($int);
200
php &gt; var_export($txt);
'300'
php &gt;
Copy linkTweet thisAlerts:
@PeterPan_321authorDec 01.2018 — @root#1598461 I think you misunderstood my question. I'm talking about SHORT files containing DATA produced by my own code, which I (or another process) may want to read later. If *I* create the data, say an integer, I can save it and recover it as binary, or I can save it and recover it as text. If it's *MY* data, I don't see any risk, except the performance hit I mentioned.

I normally would not attempt to convert an existing binary file to text, be it an executable or an image file. But there are exceptions there too. For example, if you want to attach a file to an email you generate, you have to convert it to 7-bit ASCII "Mime" encoding.
Copy linkTweet thisAlerts:
@PeterPan_321authorDec 01.2018 — @NogDog#1598463 I settled on the use of sprintf() and sscanf() to convert my small amount of binary data to ASCII text. For a simple example (which obviously needs more to be generally useful), consider saving a simple integer to a file.

[code=php]
$filePath = "somedirectory/somefile.txt";
$someInteger = 12345678;
$stringVersion = sprintf("%d" $someInteger);
$handle = fopen($filePath, "w");
if ($handle) {
$len = fputs ( $handle , $timeIntStr);
fclose($handle);
}
[/code]


That file would now contain "12345678", as a simple ASCII string.

Then to get it back elsewhere

[code=php]
$someInteger =0; // or some error code
$handle = fopen($filePath, "r");
if ($handle) {
$tmpString = fgets ( $handle);
$a = sscanf $tmpString , "%d"); // get array of returned values
fclose($handle);
$someInteger = $a[0]; // In this case, only one recovered item
}
[/code]


Again the string is read, and then converted to a real integer. That's just the basic.

I think I've used the idea of saving binarys as text back when i advocated the continued use of ".INI" files in the PC world, in place of storing everything in binary in the system registry. I could then start my project reading 9or writing) files containing ASCII keys and values like the below. These could easily be moved from machine to machine, and could be edited with a simple text editor if necessary.

maxTimeoutSeconds =60

defaultConfigutarionULR="http://mysystem/appdata/thisAppInfo.txt"

maxRetriesOnPasswordAccess=3

maximumPhotosToProcess=1000

etc,, etc., etc.
×

Success!

Help @PeterPan_321 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...