Character conversion php

WBOY
Release: 2016-07-29 09:13:54
Original
1247 people have browsed it

Character conversion php

<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
<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
<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.

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!