In PHP development, we often encounter situations where binary data needs to be read from MySQL database. Especially when binary fields (such as BINARY , VARBINARY or BIT types) are stored in the database, how to correctly convert these data into decimal values has become a relatively common problem. PHP provides a bindec function for converting binary strings into decimal integers, but you often encounter some pitfalls when using them directly.
This article will explain in detail how to correctly use the bindec function to process binary field data taken from MySQL.
Suppose the database has such a field definition:
CREATE TABLE example (
id INT PRIMARY KEY,
bin_data BINARY(4)
);
Store binary data in:
INSERT INTO example (id, bin_data) VALUES (1, 0b1011);
When querying through PDO or mysqli in PHP, the bin_data field will be returned as a string, but the string at this time is binary content, not text "1011" or "11", but the corresponding byte stream.
For example, after taking out, it may be a raw byte like "\x0B\x00\x00\x00" and the result of calling bindec directly is incorrect.
The arguments of the bindec function should be strings like "1011" . If you give it a binary byte stream directly, it will not parse the byte value, but interpret it by character, resulting in an error result.
Use PHP's built-in functions to convert them into "0" and "1" strings by byte, and then parse them with bindec .
Sample code:
<?php
// Assume from MySQL The binary field removed
$binaryData = "\x0B"; // Decimal11,Corresponding binary1011
// Convert a byte stream to a binary string
function binaryToBitString(string $binary): string {
$bitString = '';
for ($i = 0; $i < strlen($binary); $i++) {
// ord() Get characters ASCII code,Convert to8Bit binary string
$bitString .= str_pad(decbin(ord($binary[$i])), 8, '0', STR_PAD_LEFT);
}
return $bitString;
}
$bitString = binaryToBitString($binaryData);
echo "Bit string: " . $bitString . "\n";
// use bindec 转换为Decimal数
$decimalValue = bindec($bitString);
echo "Decimal value: " . $decimalValue . "\n";
?>
Output result:
Bit string: 00001011
Decimal value: 11
This will correctly convert binary data taken from MySQL into decimal numbers.
If the field is of BIT type, you can convert it to a decimal number using the MySQL function when querying, for example:
SELECT id, BIN(bin_data) AS bin_str FROM example;
Or convert it directly to an integer:
SELECT id, CONV(HEX(bin_data), 16, 10) AS decimal_value FROM example;
In this way, what you directly get on the PHP side is binary or decimal in the form of strings, avoiding the complex processing of binary byte streams.
The bindec function can only handle binary strings composed of "0" and "1" and does not support binary byte streams.
The binary fields taken from MySQL are usually byte streams and need to be converted to binary strings before using bindec .
You can write functions byte-byte conversion by yourself, or you can use function preprocessing during SQL queries.
Choose the appropriate processing plan according to the actual situation to avoid misinterpretation of binary data.