当前位置:网站首页>PHP string to binary conversion
PHP string to binary conversion
2022-07-01 09:42:00 【Squatting in the corner counting ants】
1、 Description of related functions
pack: Load data into a binary string
unpack: Unpacking data from binary strings
preg_split : Separate strings with a regular expression .
explode: Break a string into an array
join: Combine array elements into a string
2、 Example
2.1 Convert a string to binary
/**
* Convert a string to binary
* @param type $str
* @return type
*/
function StrToBin($str){
// The segmentation rules here don't understand , Why is the rule like this
$arr = preg_split('/(?<!^)(?!$)/u', $str);
//2.unpack character
foreach($arr as &$v){
//unpack: Unpack binary strings
//H: Hexadecimal string , High in the former , And pack bring into correspondence with , You can also use * To automatically identify
$temp = unpack('H*', $v);
$v = base_convert($temp[1], 16, 2);
unset($temp);
}
return join(' ',$arr);
}
2.2 Convert binary to string
/**
* Convert binary to string
* @param type $str
* @return type
*/
function BinToStr($str){
$arr = explode(' ', $str);
foreach($arr as &$v){
// Load data into a binary string
//strlen() Specify how many 16 Base string
// Because of the designation H(16 Base number , High in the former ), So the second parameter needs to make 16 Hexadecimal character string
$v = pack("H".strlen(base_convert($v, 2, 16)), base_convert($v, 2, 16));
}
return join('', $arr);
}
2.3 Test examples
// test
echo StrToBin("php Binary test ");;
echo '<br/>';
echo BinToStr("1110000 1101000 1110000 111010001011111110011011 111001011000100010110110 111001101011010110001011 111010001010111110010101");
边栏推荐
猜你喜欢
我喜欢两个男人。。。
年薪100万,在北上广深买的起房子吗?
How to realize the usage of connecting multiple databases in idel
Dspic30f6014a LCD block display
SQL learning notes (01) - basic knowledge of database
架构实战营 模块九:设计电商秒杀系统
Strange, why is the ArrayList initialization capacity size 10?
OSPF - virtual link details (including configuration commands)
电脑USB、HDMI、DP各种接口及速度
Construction of esp8266 FreeRTOS development environment
随机推荐
js valueOf 与 toString 区别
Learning practice: comprehensive application of cycle and branch structure (II)
How to realize the usage of connecting multiple databases in idel
Network partition notes
短路运算符惰性求值
js原型继承仅可继承实例而非构造器
Spark's action operator
HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴
JS use toString to distinguish between object and array
Network counting 01 physical layer
集成积木报表报错 org.apache.catalina.core.StandardContext.filterStart 启动过滤器异常
Youqitong PE toolbox [vip] v3.7.2022.0106 official January 22 Edition
主流实时流处理计算框架Flink初体验
Concept of digital currency
JS prototype trap
NoSQL数据库的安装和使用
SQL学习笔记(02)——数据库表操作
Unity tips for reducing the amount of code -- empty protection extension
dsPIC30F6014a LCD 方块显示
那个程序员,被打了。