Home Backend Development PHP Tutorial Character conversion php

Character conversion php

Jul 29, 2016 am 09:13 AM
ascii ord string

Character conversion php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

<code>图解ASCII码对照表图,以字符A为例

Dec表示十进制,如<span>65</span>

Hx表示十六进制,如<span>41</span>

Oct表示八进制,如<span>101</span>

Char表示显示字符,如A

 

ASCII码对照表图分为两个单元

<span>1</span>,控制字符 <span>0</span>-<span>31</span>和<span>127</span><span>2</span>,可显示字符 <span>32</span>-<span>126</span>

(<span>1</span>)<span>48</span>~<span>57</span>为<span>0</span>到<span>9</span>十个阿拉伯数字;

(<span>2</span>)<span>65</span>~<span>90</span>为<span>26</span>个大写英文字母;

(<span>3</span>)<span>97</span>~<span>122</span>号为<span>26</span>个小写英文字母;

(<span>4</span>)其它标点符号、运算符号等;

 

三,PHP字符转换函数说明

 

具体字符转换函数说明请参考[PHP函数篇详解十进制、二进制、八进制和十六进制转换函数说明]

十进制转二进制 decbin() 函数

十进制转八进制 decoct() 函数

十进制转十六进制 dechex() 函数

二进制转十六制进 bin2hex() 函数

二进制转十制进 bindec() 函数

八进制转十进制 octdec() 函数

十六进制转十进制 hexdec()函数

任意进制转换 base_convert() 函数

 

字符转换实例

实例一,如何把一个字符转换为二进制、八进制或十六进制,可以使用ord()函数先把字符转换为ASCII值,然后使用相应的进制转换函数进行转换,如下

a 这个字符转换为其二进制/八进制/十六进制,如下

a字符的十进制:ord(<span>'a'</span>); <span>//输出97</span>

二进制:decbin(ord(<span>'a'</span>)); <span>//输出1100001</span>

八进制:decoct(ord(<span>'a'</span>)); <span>//输出141</span>

十六进制:dechex(ord(<span>'a'</span>)); <span>//输出61</span>然后可以通过把各进制输出的结果对应上面ASCII码对照表图进行核对。

 

实例二,如何把一个二进制转换为十六进制或十进制,如a的二进制,如下

采用实例一的方法获取a字符的二进制

decbin(ord(<span>'a'</span>));

然后把二进制转换为十六进制或十进制

十六进制:bin2hex(decbin(ord(<span>'a'</span>)));<span>//输出31313030303031</span>

二进制J:bindec(decbin(ord(<span>'a'</span>))); <span>//输出97</span></code>

Copy after login

1

2

3

4

5

6

7

8

9

10

11

12

<code><span>1</span>.chr()函数

该函数用于将ASCII码值转化为字符串。其函数声明如下:

string chr (int ascii);

<span>2</span>.ord()函数

该函数用于将字符串转化为ASCII码值。其函数声明如下:

int ord(string str);

示例:

使用chr()函数和ord()函数进行字符串与ASCII码之间的转换,程序代码如下:

<span><?php </span><span>$str1</span>=chr(<span>88</span>);

<span>echo</span><span>$str1</span>;               <span>//返回值为X</span><span>echo</span><span>"\t"</span>;

<span>$str2</span>=ord(<span>'S'</span>);

<span>echo</span><span>$str2</span>;               <span>//返回值为83</span><span>?></span></span></code>

Copy after login

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

<code>**

bin2hex() 函数把 ASCII 字符的字符串转换为十六进制值。

bin2hex(string)

     参数              描述

    string        必需。要转换的字符串。

 

字符串可通过使用 pack() 函数再转换回去。

pack() 函数把数据装入一个二进制字符串。

pack(format,args+)

     参数                描述

    format     必需。规定在包装数据时所使用的格式。

    args+      可选。规定被包装的一个或多个参数。

 

**

 

<span>$string</span> = <span>"Hello\tworld!\n"</span>;

<span>print</span>(<span>$string</span>.<span>"\n"</span>);

<span>print</span>(bin2hex(<span>$string</span>).<span>"\n"</span>);

====================================================

---- 输出的结果:

----

---- Hello world!

----

---- <span>48656</span>c6c6f09776f726c64210a

====================================================

 

把 <span>"Shanghai"</span> 转换为十六进制值:

<span><?php </span><span>$str</span> = <span>"Shanghai"</span>;

    <span>echo</span> bin2hex(<span>$str</span>) . <span>"<br>"</span>;

    <span>echo</span> pack(<span>"H*"</span>,bin2hex(<span>$str</span>)) . <span>"<br>"</span>;

 

<span>?></span>

====================================================

---- 输出的结果:

---- <span>5368616e67686169</span>

---- Shanghai

====================================================

 

 

 

 

 

</span></code>

Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

Copyright Statement: Everything is for learning

The above introduces the character conversion PHP, including aspects of the content, I hope it will be helpful to friends who are interested in PHP tutorials.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Convert basic data types to strings using Java's String.valueOf() function Convert basic data types to strings using Java's String.valueOf() function Jul 24, 2023 pm 07:55 PM

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

How to convert char array to string How to convert char array to string Jun 09, 2023 am 10:04 AM

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

How many bytes does one ascii character occupy? How many bytes does one ascii character occupy? Mar 09, 2023 pm 03:49 PM

One ascii character occupies 1 byte. ASCII code characters are represented by 7-bit or 8-bit binary encoding in the computer and are stored in one byte, that is, one ASCII code occupies one byte. ASCII code can be divided into standard ASCII code and extended ASCII code. Standard ASCII code is also called basic ASCII code. It uses 7-bit binary numbers (the remaining 1 binary digit is 0) to represent all uppercase and lowercase letters, and the numbers 0 to 9. Punctuation marks, and special control characters used in American English.

Use Java's String.replace() function to replace characters (strings) in a string Use Java's String.replace() function to replace characters (strings) in a string Jul 25, 2023 pm 05:16 PM

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

2w words detailed explanation String, yyds 2w words detailed explanation String, yyds Aug 24, 2023 pm 03:56 PM

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.

Quickly learn about ASCII value conversion in PHP Quickly learn about ASCII value conversion in PHP Mar 28, 2024 pm 06:42 PM

ASCII value conversion in PHP is a problem often encountered in programming. ASCII (American Standard Code for Information Interchange) is a standard encoding system for converting characters into numbers. In PHP, we often need to convert between characters and numbers through ASCII code. This article will introduce how to convert ASCII values ​​in PHP and give specific code examples. 1. Change the characters

Use java's String.length() function to get the length of a string Use java's String.length() function to get the length of a string Jul 25, 2023 am 09:09 AM

Use Java's String.length() function to get the length of a string. In Java programming, string is a very common data type. We often need to get the length of a string, that is, the number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string. Here is a simple example code: publicclassStringLengthExample{publ

How to use java's String class How to use java's String class Apr 19, 2023 pm 01:19 PM

1. Understanding String1. String in JDK First, let’s take a look at the source code of the String class in the JDK. It implements many interfaces. You can see that the String class is modified by final. This means that the String class cannot be inherited and there is no subclass of String. class, so that all people using JDK use the same String class. If String is allowed to be inherited, everyone can extend String. Everyone uses different versions of String, and two different people Using the same method shows different results, which makes it impossible to develop the code. Inheritance and method overriding not only bring flexibility, but also cause many subclasses to behave differently.

See all articles