PHP and ASCII code

Source: Internet
Author: User
Tags binary to decimal bit set decimal to binary

First, let's briefly talk about the history. The ASCII code was first developed by Americans. What is it? We know that the computer only knows 0 and 1. If we want the computer to recognize characters other than 01, for example, 'A', we must first tell the computer that '123' is 'A '. Just like the moss password, it means SOS ). The so-called character encoding means to intercept with the computer and agree with the computer What character a series of 0101 represents.

ASCII encoding is the first character. However, since ASCII only has 8 bits and actually uses 7 bits, it can only represent 128 characters (00000000 ~ 01111111 ). These 128 characters include printable common characters and non-printable command characters. See the following table. The 128 characters should be enough for Americans. But for countries and regions that use non-English characters, such as Latin, text, Russian, and Chinese, this is definitely not enough. So some people developed extended ASCII and new character encoding, which was extended to 16-bit and 32-bit based on the original 8-bit. Our common isoxxxx, gb2312, GBK, Big 5, Unicode... are all developed later. Although
Some character encodings are incompatible. For example, for Chinese characters, gb2312 is incompatible with Unicode. That is to say, the binary values of the underlying conventions are different for the same Chinese character. Garbled on the web page
This is the reason. However, these subsequently developed character encodings are backward compatible with ASCII. This is why UTF-8 (UNICODE) and GBK/gb2312 are not garbled in English,
Chinese characters may be garbled. Because they are both compatible with ASCII, the display of 52 letters is based on ASCII. However, ASCII is not Chinese, so Unicode and GBK all press
Their respective standards are explained. First, let's look at the ASCII standard encoding. Find out the binary, decimal, octal, hexadecimal, and character representation and conversion in PHP. First, let's look at some notation: Dec: decimal HEX: hexadecimal Oct: octal bin: Binary so the number 16 is represented: 16D = 01 H = 018o = 1000 then B. All of the above are some representations. It is easy for people to see. ========================================================== ========================================================== ======================== Now there is a demand, in Program Input a hexadecimal number, Display the corresponding ASCII characters on the computer . For example, in 41h, A is matched. Method 1: Use Escape characters : \ Xdd and \ DDD are provided in the escape characters, \ Xdd indicates hexadecimal, and \ DDD indicates octal. So we can: [PHP] View plaincopy
    1. <? PHP
    2. Echo "\ X41";// Hexadecimal, starting with \ x
    3. Echo "/101";// Gossip, just a few
    4. ?>

 

Note: Only hexadecimal and octal characters are allowed to be converted to escape characters, but binary and decimal characters are not supported. So we need to use another method. Method 2: Use CHR () function . This function parameter is very simple, that is, the ASCII code corresponding to the decimal, hexadecimal, octal. Decimal: directly write the integer octal: The first digit must be 0. Hexadecimal: 0xdd binary: String Note: The hexadecimal value must be 0xdd. [PHP] View plaincopy
  1. <? PHP
  2. Echo CHR(65 );// In decimal format, because all values are input, double quotation marks are unnecessary.
  3. Echo CHR(0x41 );// Hexadecimal
  4. Echo CHR(0101 );// Octal, three-digit, with the highest bit set to zero
  5. Echo CHR('123');// Binary. Note that the binary here must be a string with quotation marks! None of the above
  6. ?>

 

Method 3 use Printf/sprintf () function Format output [PHP] View plaincopy
  1. <? PHP
  2. Printf ("% C", 0x41 );// The second parameter must not contain single or double quotation marks.
  3. Printf ("% C", 0101 );// The second parameter must not contain single or double quotation marks.
  4. Printf ("% C", 65 );// The second parameter must not contain single or double quotation marks.
  5. Printf ("% C",'123');
  6. ?>

 

The second parameter format is basically the same as that of CHR (). There are no quotation marks in octal, decimal, or hexadecimal notation, and quotation marks are required in binary notation. The first part of the 8-digit system is 0, and the first part of the 16-digit system is 0x.
========================================================== ========================================================== Method 1: ord () function [PHP] View plaincopy
    1. <? PHP
    2. EchoOrd ('A');
    3. ?>

 

The output is 65. Therefore, this function can only be converted from characters Decimal. Note: This method only extracts the first character. If it is a string, you cannot process the subsequent characters. Method 2: bin2hex () function: converts a [String] Hexadecimal [PHP] View plaincopy
    1. <? PHP
    2. EchoBin2hex ('A');
    3. ?>

 

The output result is 41. Note that this 41 is only a literal value and has no hexadecimal significance. This function cannot be directly transferred to hexadecimal notation using a binary '192... '. It can only be converted from a string to hexadecimal notation. This function can process strings. Printf/sprintf () function No. Therefore, you can only convert it to decimal, binary, and octal values by using ord. (Check the pack function later) ========================================================== ========================================================== ==== conversion of various hexadecimal formats: bindec (): Binary to decimal [PHP] View plaincopy
    1. <? PHP
    2. Echo Bindec(1100 );// OUTPUT 12, which can be enclosed by quotation marks or not added
    3. ?>

 

Decbin (): Convert decimal to binary.
Dechex (): Convert decimal to hexadecimal Hexdec (): hexadecimal to hexadecimal (directly write the parameter value without writing 0x)
Octdec (): Convert octal to decimal Decoct (): 10 hexadecimal to 8 hexadecimal
Base_convert (number to be converted, original, target): Any hexadecimal conversion <? PHP Echo base_convert (123, 6, 2) ;?> Note: The above conversion functions are all mathematical processing functions, so the parameters do not need to be in the string form (no quotation marks are required), just write the value directly.

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.