Strings in PHP are always BLOBs. So you can use a string to hold the value for your database BLOB. All of this stuff base-converting and so on has to do with presenting that BLOB.
If you want a nice human-readable representation of your BLOB then it makes sense to show the bytes it contains, and probably to use hex rather than decimal. Hence, the string "41 42 43" is a good way to present the byte array that in C# would be
var bytes = new byte[] { 0x41, 0x42, 0x43 };
but it is obviously not a good way to represent those bytes! The string "ABC" is an efficient representation, because it is in fact the same BLOB (only it's not so Large in this case).
In practice you will typically get your BLOBs from functions that return string - such as that hashing function, or other built-in functions like fread.
In the rare cases (but not so rare when just trying things out/prototyping) that you need to just construct a string from some hard-coded bytes I don't know of anything more efficient than converting a "hex string" to what is often called a "binary string" in PHP:
$myBytes = "414243";
$data = pack('H*', $myBytes);
If you var_dump($data);
it'll show you string(3) "ABC"
. That's because 0x41 = 65 decimal = 'A' (in basically all encodings).
Since looking at binary data by interpreting it as a string is not exactly intuitive, you may want to make a basic wrapper to make debugging easier. One possible such wrapper is
class blob
{
function __construct($hexStr = '')
{
$this->appendHex($hexStr);
}
public $value;
public function appendHex($hexStr)
{
$this->value .= pack('H*', $hexStr);
}
public function getByte($index)
{
return unpack('C', $this->value{$index})[1];
}
public function setByte($index, $value)
{
$this->value{$index} = pack('C', $value);
}
public function toArray()
{
return unpack('C*', $this->value);
}
}
This is something I cooked up on the fly, and probably just a starting point for your own wrapper. But the idea is to use a string for storage since this is the most efficient structure available in PHP, while providing methods like toArray() for use in debugger watches/evaluations when you want to examine the contents.
Of course you may use a perfectly straightforward PHP array instead and pack it to a string when interfacing with something that uses strings for binary data. Depending on the degree to which you are actually going to modify the blob this may prove easier, and although it isn't space efficient I think you'd get acceptable performance for many tasks.
An example to illustrate the functionality:
// Construct a blob with 3 bytes: 0x41 0x42 0x43.
$b = new blob("414243");
// Append 3 more bytes: 0x44 0x45 0x46.
$b->appendHex("444546");
// Change the second byte to 0x41 (so we now have 0x41 0x41 0x43 0x44 0x45 0x46).
$b->setByte(1, 0x41); // or, equivalently, setByte(1, 65)
// Dump the first byte.
var_dump($b->getByte(0));
// Verify the result. The string "AACDEF", because it's only ASCII characters, will have the same binary representation in basically any encoding.
$ok = $b->value == "AACDEF";