


How to solve the 16-bit character garbled problem of PHP md5 function
php md5 16-bit character garbled solution: 1. Convert the output 16-byte binary into hexadecimal; 2. Use "substr(md5($str),8,16)" Method to obtain the 16-character md5 ciphertext.
Recommended: "PHP Video Tutorial"
garbled characters
PHP's md5 function is used to perform md5 operations on string parameters. This function has two parameters:
md5 ( string $str [, bool $raw_output = FALSE ] ) : string
The first parameter is the input string; the second parameter defaults to FALSE. When set to TRUE, a 16-bit md5 value can be output.
By default,
md5(string $str)
will return: a 32-character, hexadecimal hash value.
If you add the second parametermd5(string $str,TRUE)
, it will return: a hash value in original binary format with a length of 16 bytes.
From this we can see that when a binary format of 16 bytes length (corresponding to 16 characters, because it conforms to ASCII) is returned, since the browser characterizes it, it Garbled characters will be produced:
$str = "PHP"; echo "字符串:".$str."<br>"; echo "TRUE - 原始 16 字符二进制格式:".md5($str,TRUE)."<br>"; echo "FALSE - 32 字符十六进制格式:".md5($str)."<br>";
Solution
So how to get a 16-bit md5 value that is not garbled? There are two methods:
- Convert the output 16-byte binary into hexadecimal;
- In the ciphertext of md5, the difference between 16-bit ciphertext and 32-bit ciphertext The 8th to 24th substrings are the same, so we can obtain the 16-character md5 ciphertext by intercepting:
substr(md5($str),8,16)
. The second parameter and the third parameter represent starting from the 8th character (the subscript starts from 0), taking 16 characters.
Here we use the second method to solve the problem of garbled characters. Still using the above example:
$str = "PHP"; echo "字符串:".$str."<br>"; echo "TRUE - 原始 16 字符二进制格式(乱码):".md5($str,TRUE)."<br>"; echo "TRUE - 原始 16 字符二进制格式(不乱码):".substr(md5($str),8,16)."<br>"; echo "FALSE - 32 字符十六进制格式:".md5($str)."<br>";
Note: If you need an uppercase md5 value, just use the strtoupper(...) function directly.
The above is the detailed content of How to solve the 16-bit character garbled problem of PHP md5 function. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics









