转换的1和0字符串转换成二进制值,那么COM preSS之后,PHP [英] Converting string of 1s and 0s into binary value, then compress afterwards ,PHP

查看:330
本文介绍了转换的1和0字符串转换成二进制值,那么COM preSS之后,PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字符串,例如
我COM $ P $与gzcom preSS pssing它,但它对应的ASCII码COM presses。
我想COM preSS字符串就好像它是二进制数据不是ASCII二进制数。

I have a string for example: "10001000101010001" in PHP I am compressing it with gzcompress, but it compresses ASCII equivalent. I would like to compress the string as if it were binary data not it ASCII binary equivalent.

Bascially我有2个问题:

Bascially I have 2 problems:


  1. 如何1和0的列表转换成二进制

  2. COM preSS与gzcom preSS
  3. 生成的二进制
  1. how to convert a list of 1s and 0s into binary
  2. compress the resulting binary with gzcompress

先谢谢了。

推荐答案

看看在 bindec()功能。

基本上,你要像(干法codeD,请提前测试它自己盲目信任吧)

Basically you'll want something like (dry-coded, please test it yourself before blindly trusting it)

function binaryStringToBytes($binaryString) {
    $output = '';
    for($i = 0; $i < strlen($binaryString); $i += 8) {
        $output .= chr(bindec(substr($binaryString, $i, 8)));
    }
    return $output;
}

把你变成一个字节的字符串指定格式的字符串,之后就可以 gzcom $ P $(PSS)它的意愿。

互补的功能是一样的东西。

The complementary function is something like

function bytesToBinaryString($byteString) {
    $out = '';
    for($i = 0; $i < strlen($byteString); $i++) {
        $out .= str_pad(decbin(ord($byteString[$i])), 8, '0', STR_PAD_LEFT);
    }
    return $out;
}

这篇关于转换的1和0字符串转换成二进制值,那么COM preSS之后,PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆