The Little Guy Posted November 3, 2010 Share Posted November 3, 2010 Is this the correct way to write binary data to a file? $string = 'this is a string'; $handle = fopen('b.dat', 'wb'); for($i=0;$i<strlen($string);$i++){ $hx = bin2hex($string{$i}); fwrite($handle, pack("H*", $hx)); } If so, how do I read it an convert it back to a string? If it is not correct, how do I do it? Link to comment https://forums.phpfreaks.com/topic/217687-write-binary-data-to-a-file/ Share on other sites More sharing options...
btherl Posted November 3, 2010 Share Posted November 3, 2010 I would have gone for fwrite($handle, $string); and cut out the middle man. Link to comment https://forums.phpfreaks.com/topic/217687-write-binary-data-to-a-file/#findComment-1130097 Share on other sites More sharing options...
The Little Guy Posted November 3, 2010 Author Share Posted November 3, 2010 But that doesn't save it as binary data, it just opens the file in binary mode. Link to comment https://forums.phpfreaks.com/topic/217687-write-binary-data-to-a-file/#findComment-1130100 Share on other sites More sharing options...
btherl Posted November 4, 2010 Share Posted November 4, 2010 I don't think I understand what you are trying to do. You can use fwrite() to write any string directly to a file, including strings containing what I would call "binary data". Do you want to encode the string somehow before writing it? If so, what encoding do you want to use? Link to comment https://forums.phpfreaks.com/topic/217687-write-binary-data-to-a-file/#findComment-1130123 Share on other sites More sharing options...
The Little Guy Posted November 4, 2010 Author Share Posted November 4, 2010 I am assuming like this: http://us.php.net/manual/en/function.fwrite.php#53622 Then, if you were to open the file in an editor, it would be almost unreadable, containing wingding looking characters Link to comment https://forums.phpfreaks.com/topic/217687-write-binary-data-to-a-file/#findComment-1130135 Share on other sites More sharing options...
gizmola Posted November 4, 2010 Share Posted November 4, 2010 Yeah, but what is your goal here. You have an ascii string in your example. Are you just trying to obfuscate the data? You could convert to base64 or something like that if you wanted to accomplish that. Usually people use these functions because they have to move data back and forth from php and some application that has a specific data format. Link to comment https://forums.phpfreaks.com/topic/217687-write-binary-data-to-a-file/#findComment-1130144 Share on other sites More sharing options...
The Little Guy Posted November 4, 2010 Author Share Posted November 4, 2010 Basically, I want to try and write my own Database (mostly for kicks and giggles). So the table, I was hoping to make it look like an encrypted binary file. Maybe I am going about this the wrong way though... What do you suggest? Link to comment https://forums.phpfreaks.com/topic/217687-write-binary-data-to-a-file/#findComment-1130228 Share on other sites More sharing options...
btherl Posted November 4, 2010 Share Posted November 4, 2010 Aha.. then you're looking for either obfuscation or encryption. If it's just for fun you could use mcrypt() with a basic algorithm like 3DES in ECB mode. That keeps things simple. Except that the decrypted data is padded with null bytes up to the block size of the algorithm used - you'll need to remove those bytes after decrypting. Link to comment https://forums.phpfreaks.com/topic/217687-write-binary-data-to-a-file/#findComment-1130372 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.