The difference between characters and bytes in PHP and the conversion of strings and bytes into text

Source: Internet
Author: User
Characters in PHP are abstract entities that can be represented using a variety of different character schemes or code pages. Bytes are units that transmit information over the network (or store information on a hard disk or in memory). In this article, we also introduce the example of string and byte conversion in PHP, and look at the friends who are interested.

Character:

a character is an abstract entity that can be represented using a variety of different character schemes or code pages . For example, a Unicode UTF-16 encoding represents a character as a sequence of 16-bit integers, whereas a Unicode UTF-8 encoding represents the same character as a 8-bit byte sequence. The common language runtime uses Unicode UTF-16 (Unicode conversion format, 16-bit encoded form) to represent characters.

PHP under the UTF-8 encoding, a Chinese character accounted for 3 characters, GBK encoded only 2 characters.

Byte (byte):

Bytes are units that transmit information over the network (or store information on a hard disk or in memory).

Application:

If the table in the database is UTF8 encoded, the field is set to the maximum of 10 characters, and the max deposit length is eg: ' I am 1 '

Here's a look at the PHP string and byte conversion example

<?php/** * byte array with string conversion class */class Bytes {/** * Converts a string string to a byte array * @param $str the string to be converted * @param $bytes destination byte array * @author Zikie */public static function GetBytes ($string) {$bytes = array (); for ($i = 0; $i < strlen ($string); $i + +) {$ bytes[] = Ord ($string [$i]); } return $bytes; /** * Converts a byte array to a string type of data * @param $bytes byte array * @param $str target String * @return A String type of data */public static function toSt R ($bytes) {$str = '; foreach ($bytes as $ch) {$str. = Chr ($ch);} return $str;}/** * Converts an int to a byte array * @param $byt target by  Te Array * @param $val the string to be converted * */public static function integertobytes ($val) {$byt = array (); $byt [0] = ($val & 0xff); $BYT [1] = ($val >> 8 & 0xff); $BYT [2] = ($val >> & 0xff); $BYT [3] = ($val >> & 0xff); return $byt; }/** * Reads an integer type of data from the location specified in the byte array * @param $bytes byte array * @param $position the specified start position * @return An integer type of data */public stat IC function Bytestointeger ($bytes, $position) {$val = 0; $val = $bytes [$position + 3]& 0xFF; $val <<= 8; $val |= $bytes [$position + 2] & 0xFF; $val <<= 8; $val |= $bytes [$position + 1] & 0xFF; $val <<= 8; $val |= $bytes [$position] & 0xFF; return $val; }/** * Converts a shor string to a byte array * @param $byt destination byte array * @param $val the string to be converted * */public static function shorttobytes ($val) {$ byt = Array (); $byt [0] = ($val & 0xff); $BYT [1] = ($val >> 8 & 0xff); return $byt; /** * Reads a short type of data from the location specified in the byte array. * @param $bytes byte array * @param $position the specified start position * @return A short type of data */public static function Bytestoshort ($bytes, $posi tion) {$val = 0; $val = $bytes [$position + 1] & 0xFF; $val = $val << 8; $val |= $bytes [$position] & 0xFF; r Eturn $val; }}?>

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.