The difference between characters and bytes in PHP and detailed explanation of string and byte conversion

墨辰丷
Release: 2023-03-28 18:48:02
Original
1904 people have browsed it

Characters in php are abstract entities that can be represented using a variety of different character schemes or code pages. A byte is a unit for transmitting information over a network (or storing information on a hard drive or in memory). This article also introduces examples of string and byte conversion in PHP through examples. Friends who are interested should take a look together

Characters:

Characters are abstract entities that can be represented using many different character schemes or code pages. For example, Unicode UTF-16 encoding represents characters as a sequence of 16-bit integers, while Unicode UTF-8 encoding represents the same characters as a sequence of 8-bit bytes. The common language runtime uses Unicode UTF-16 (Unicode Transformation Format, a 16-bit encoding) to represent characters.

In php, under UTF-8 encoding, a Chinese character occupies 3 characters, and under gbk encoding, it only occupies 2 characters.

Byte (Byte):

Byte is to transmit information through the network (or store information on the hard disk or memory) The unit.

Application:

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

Let’s look at the PHP string and byte conversion example

<?php 
/** 
* byte数组与字符串转化类 
*/
class Bytes { 
/** 
* 转换一个String字符串为byte数组 
* @param $str 需要转换的字符串 
* @param $bytes 目标byte数组 
* @author Zikie 
*/
public static function getBytes($string) { 
$bytes = array(); 
for($i = 0; $i < strlen($string); $i++){ 
$bytes[] = ord($string[$i]); 
} 
return $bytes; 
} 
/** 
* 将字节数组转化为String类型的数据 
* @param $bytes 字节数组 
* @param $str 目标字符串 
* @return 一个String类型的数据 
*/
public static function toStr($bytes) { 
$str = &#39;&#39;; 
foreach($bytes as $ch) { 
$str .= chr($ch); 
} 
return $str; 
} 
/** 
* 转换一个int为byte数组 
* @param $byt 目标byte数组 
* @param $val 需要转换的字符串 
* 
*/
public static function integerToBytes($val) { 
$byt = array(); 
$byt[0] = ($val & 0xff); 
$byt[1] = ($val >> 8 & 0xff); 
$byt[2] = ($val >> 16 & 0xff); 
$byt[3] = ($val >> 24 & 0xff); 
return $byt; 
} 
/** 
* 从字节数组中指定的位置读取一个Integer类型的数据 
* @param $bytes 字节数组 
* @param $position 指定的开始位置 
* @return 一个Integer类型的数据 
*/
public static 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; 
} 
/** 
* 转换一个shor字符串为byte数组 
* @param $byt 目标byte数组 
* @param $val 需要转换的字符串 
* 
*/
public static function shortToBytes($val) { 
$byt = array(); 
$byt[0] = ($val & 0xff); 
$byt[1] = ($val >> 8 & 0xff); 
return $byt; 
} 
/** 
* 从字节数组中指定的位置读取一个Short类型的数据。 
* @param $bytes 字节数组 
* @param $position 指定的开始位置 
* @return 一个Short类型的数据 
*/
public static function bytesToShort($bytes, $position) { 
$val = 0; 
$val = $bytes[$position + 1] & 0xFF; 
$val = $val << 8; 
$val |= $bytes[$position] & 0xFF; 
return $val; 
} 
} 
?>
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHP Gets the image width, height, size, image type, img attribute used for layout

# #7 ways to use PHP header()

PHP namespace (namespace)

The above is the detailed content of The difference between characters and bytes in PHP and detailed explanation of string and byte conversion. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!