Let's start with the analysis. Let's first look at the introduction of this function. stringhex2bin (string $ data) if the input hexadecimal string is an odd number or invalid hexadecimal string, an error at the E_WARNING level is thrown. First look at an example? Php $ hexhex2bin (6578616d706c65206865782064617461); var_dump (
Let's start with the analysis. Let's first look at the introduction of this function. string hex2bin (string $ data) if the input hexadecimal string is an odd number or invalid hexadecimal string, an error at the E_WARNING level is thrown. First look at an example? Php $ hex = hex2bin (6578616d706c65206865782064617461); var_dump (
Let's start with the analysis.
Let's take a look at this function.
StringHex2bin(String$data
)
If the input hexadecimal string is an odd long number or an invalid hexadecimal stringE_WARNING
Level error.
Let's look at an example.
$ Hex = hex2bin ("6578616d706c65206865782064617461 ");
Var_dump ($ hex );
?>
The output of the preceding routine is similar:
string(16) "example hex data"
You should understand.
1. Loop the entire string
2. Each two hexadecimal symbols form an ascii character, for example, 65 = 0x6 <4 + 5 = e
3. output results
I think so.
Because we have to determine whether the input string is valid, we have added some judgments. What is the character range?
Another key point is why we need to subtract 0 and 87, because we need to convert hexadecimal characters into real numbers, so we need to subtract
However, the above program is written incorrectly. I think it can be optimized.
Let's see how the php kernel is written.
We can see that,
1. The author does not use the high and low variables, but uses str [I] instead to accumulate the values. Note that the author first shifts and then adds the values. The program I wrote is in place in one step, this is also a skill.
2. Another point is j + = 2, which is replaced by two j ++.
3. The addition is replaced by the or operation, which is faster than ours. In what cases can we use the or operation? That is, the low position of the original number is 0, plus any number, the two numbers can be considered to be different operations, for example, 0xf0 | 0x0f = 0xff = 0xf0 + 0x0f.