Develop php kernel functions, Article 2 hex2bin

Source: Internet
Author: User
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_WARNINGLevel 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.