Is the MySQL BIT_LENGTH() function multibyte safe?

WBOY
Release: 2023-09-16 20:21:06
forward
773 people have browsed it

MySQL BIT_LENGTH() 函数是否是多字节安全的?

Just like LENGTH() function, MySQL BIT_LENGTH() function is not a multi-byte safe function. As we know that the difference of the result between multi-byte safe functions, like CHAR_LENGTH() or CHARACTER_LENGTH(), and BIT_LENGTH() function especially relevant for Unicode, in which most of the characters are encoded in two bytes or relevant for UTF-8 where the number of bytes varies. It is demonstrated in the example below −

Example

mysql> Select BIT_LENGTH('tutorialspoint'); +------------------------------+ | BIT_LENGTH('tutorialspoint') | +------------------------------+ | 112 | +------------------------------+ 1 row in set (0.00 sec)
Copy after login

The above result set shows that the bit length of string ‘tutorialspoint’ is 112 because it is yet not converted to Unicode character. The following query converts it into Unicode character −

mysql> SET @A = CONVERT('tutorialspoint' USING ucs2); Query OK, 0 rows affected (0.02 sec)
Copy after login

在将字符串转换为Unicode后,结果为224而不是112,因为在Unicode中,一个字符占用2个字节,如下所示 −

mysql> Select BIT_LENGTH(@A); +----------------+ | BIT_LENGTH(@A) | +----------------+ | 224 | +----------------+ 1 row in set (0.00 sec)
Copy after login

The above is the detailed content of Is the MySQL BIT_LENGTH() function multibyte safe?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!