


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version
Chinese version, very easy to use
