seperate binary retrieved from sql server

Hello,
I’ve finally managed to send my custom binary data to my server as an SQL BLOB.
i’ve used a form which contains ‘ID’(as string) , ‘gamesave_backup’(as binary) , ‘achievements_backup’(as binary)
the server place them in their positions correctly,
but whenever I want to retrive them back, the server sends both binaries as one bigger binary file.
everything works as expected. the only problem is that how should I separate two binary data from each other? should I split that in unity just like strings? or maybe should I use two differend php codes to send back binary datas individually?

$uniqueID = $_POST["uniqueID"];

$server = 'localhost';
$username = 'username';
$password = 'password';
$databaseName = 'database';
$tableName = 'table';

$select   = "SELECT * FROM $tableName WHERE uniqueID='$uniqueID'";

$db = mysql_connect($server,$username,$password)or die('could not connect to database!: ' .mysql_error());
$result = mysql_select_db($databaseName) or die('Could not select database');
$result.mysql_query($select,$db);

while ($row = mysql_fetch_array($result))
    echo $row['uniqueID']   . ":"
        . $row['save_bkp'] . ":"
        . $row['achievements_bkp']  . "

";

// Close the connection, we're done here.

mysql_close($connection);

You currently mix binary data with text. However binary data can represent any possible characters between 0 and 255. You send back a concatenated string where you seperated your three parts using a colon character. However this is dangerous since your binary data could contain a colon (decimal 58, hex: 0x3A). So you don’t have a chance to actually split your “text” according to your used seperator.

Generally there are several techniques possible.

First you can encode your binary data in a human readable format such as base64. This however will increase the number of bytes that need to be transferred. The base64 representation is 1/3rd longer than the original data. So a 300 byte chunk of data would become 400 bytes.

Another way is to not use “text” but to encode all your data in a binary format. For this you would need to send the size of each of your “parts” as a 4 byte integer for example. To encode the length as a 4 byte integer you may use the pack function in PHP with the format string “L”. In Unity you can use the BinaryReader to actually read the data. Keep in mind that you have to read the data in the exact same order that you write that data. So of course the length information has to come before the actual data. This also might have issues with different byte orders between systems so it might not be the best solution.

Another way would be to still interpret the result as “text” but use an escape character in your binary data. A common escape character would be the backslash "". However that means you have to pre-process your binary data and replace all occurrences of backslashes and your seperation characers (colon and newline) with an “escaped version”. So a backslash in the data need to be replaced by “\” and a colon by something like “\c” and a newline character by "
". When reading the data in C# you can savely split the string at the newline and colon characters since the binary data doesn’t contain any of those anymore. However you have to “unescape” the data on the C# side. So you simply search through the data and replace any “\c” with “:”, any "
" with "
" and any “\” with "".

Since an HTTP request already has quite a bit of overhead for relatively small amounts of binary data you can just use a base64 encoding. Of course it should also be possible to simply encode all your data in a JSON string. JSON actually uses backslash escaping inside string values. So any json parser should do all the unescaping work for you. PHP has the json_encode function to convert an array data structure to json. I’m not really familiar with PHP but i think you could actually pack all your "$row"s into an array and convert the result to json. This should give you something like

[
    {"uniqueID" : "YOUR_ID", "save_bkp" : "BINARY_DATA", "achievements_bkp" : "BINARY_DATA"},
    {"uniqueID" : "YOUR_ID", "save_bkp" : "BINARY_DATA", "achievements_bkp" : "BINARY_DATA"},
    {"uniqueID" : "YOUR_ID", "save_bkp" : "BINARY_DATA", "achievements_bkp" : "BINARY_DATA"}
]