Home php教程 php手册 php bcd码压缩-把十进制数字压缩到十六进制数据中

php bcd码压缩-把十进制数字压缩到十六进制数据中

May 25, 2016 pm 04:50 PM
decimal compression number

下面我们一起来看和篇关于php bcd码压缩-把十进制数字压缩到十六进制数据中实例,希望文章给各位同学带来帮助哦。

例,php bcd码压缩-把十进制数字压缩到十六进制数据中,代码如下:

<?php
/*
php bcd码压缩-把十进制数字压缩到十六进制数据中  
例如 0091 压缩后 0x00 0x91  
*/
$string = &#39;0091&#39;;
$bytes = Bytes::getBytes($string);
print_r($bytes);
/*
Array  
(  
 
[0] => 48  
 
[1] => 48  
 
[2] => 57  
 
[3] => 49  
)  
*/
$asc = Bytes::AscToHex($bytes, 4);
//4位压缩成2位
print_r($asc);
/*
Array  
(  
 
[0] => 0  
 
[1] => 145  
)  
*/
echo Bytes::toStr($asc);
/*
0091  
*/
$hex = Bytes::HexToAsc($asc, 2);
//反操作2位还原成4位
print_r($hex);
/*
Array  
(  
 
[0] => 48  
 
[1] => 48  
 
[2] => 57  
 
[3] => 49  
)  
*/
?>
Copy after login

例,把十进制数字压缩到十六进制数据中,代码如下:

<?php
/**  
 * php bcd码压缩
 * 把十进制数字压缩到十六进制数据中
 * @author phpff.com
 * Created on 2011-7-15
 */
class Bytes {
    /**  
     * 转换一个String字符串为byte数组
     * @param $str 需要转换的字符串
     * @param $bytes 目标byte数组
     * @author phpff.com
     */
    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.= bin2hex(chr($ch));
        }
        return $str;
    }
    /**  
     * asc码转成16进制数据
     * @param $asc asc数字字符串
     * @param $AscLen 需要转换的字符串长度
     * @return 16进制数组
     * @author  phpff.com
     */
    public static function AscToHex($asc, $AscLen) {
        $i = 0;
        $Hex = array();
        for ($i = 0; 2 * $i < $AscLen; $i++) {
            /*A:0x41(0100 0001),a:0x61(0110 0001),右移4位后都是0001,加0x90等0xa*/
            $Hex[$i] = (chr($asc[2 * $i]) << 4);
            if (!(chr($asc[2 * $i]) >= &#39;0&#39; && chr($asc[2 * $i]) <= &#39;9&#39;)) {
                $Hex[$i]+= 0x90;
            }
            if (2 * $i + 1 >= $AscLen) {
                break;
            }
            $Hex[$i]|= (chr($asc[2 * $i + 1]) & 0x0f);
            if (!(chr($asc[2 * $i + 1]) >= &#39;0&#39; && chr($asc[2 * $i + 1]) <= &#39;9&#39;)) {
                $Hex[$i]+= 0x09;
            }
        }
        return $Hex;
    }
    /**  
     * 将16进制的数据转换成asc码
     * @param $Hex 16进制数组
     * @param $HexLen 16进制数组长度
     * @return asc数组
     * @author  phpff.com
     */
    public static function HexToAsc($Hex, $HexLen) {
        $i = 0;
        $Temp = 0;
        for ($i = 0; $i < $HexLen; $i++) {
            $Temp = ($Hex[$i] & 0xf0) >> 4;
            if ($Temp < 10) {
                $Asc[2 * $i] = (0x30 + $Temp);
            } else {
                $Asc[2 * $i] = (0x37 + $Temp);
            }
            $Temp = $Hex[$i] & 0x0f;
            if ($Temp < 10) {
                $Asc[2 * $i + 1] = (0x30 + $Temp);
            } else {
                $Asc[2 * $i + 1] = (0x37 + $Temp);
            }
        }
        return $Asc;
    }
}
Copy after login


教程地址:

欢迎转载!但请带上文章地址^^

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1244
24
7-zip maximum compression rate setting, how to compress 7zip to the minimum 7-zip maximum compression rate setting, how to compress 7zip to the minimum Jun 18, 2024 pm 06:12 PM

I found that the compressed package downloaded from a download website will be larger than the original compressed package after decompression. The difference is tens of Kb for a small one and several dozen Mb for a large one. If it is uploaded to a cloud disk or paid space, it does not matter if the file is small. , if there are many files, the storage cost will be greatly increased. I studied it specifically and can learn from it if necessary. Compression level: 9-Extreme compression Dictionary size: 256 or 384, the more compressed the dictionary, the slower it is. The compression rate difference is larger before 256MB, and there is no difference in compression rate after 384MB. Word size: maximum 273 Parameters: f=BCJ2, test and add parameter compression rate will be higher

Tips to reduce win10 screen recording file size Tips to reduce win10 screen recording file size Jan 04, 2024 pm 12:05 PM

Many friends need to record screens for office work or transfer files, but sometimes the problem of files that are too large causes a lot of trouble. The following is a solution to the problem of files that are too large, let’s take a look. What to do if the win10 screen recording file is too large: 1. Download the software Format Factory to compress the file. Download address >> 2. Enter the main page and click the "Video-MP4" option. 3. Click "Add File" on the conversion format page and select the MP4 file to be compressed. 4. Click "Output Configuration" on the page to compress the file according to the output quality. 5. Select "Low Quality and Size" from the drop-down configuration list and click "OK". 6. Click "OK" to complete the import of video files. 7. Click "Start" to start the conversion. 8. After completion, you can

How to compress a folder and send it in wps How to compress a folder and send it in wps Mar 20, 2024 pm 12:58 PM

Office workers use wps software very frequently at work. Sometimes they input multiple files a day and then send them to the leader or to a designated location. So how does wps software compress a folder and package it for sending? The editor below will teach you. This operation step. First, organize the files and folders you want to send into the same folder. If you have a lot of files, it's a good idea to name each file so it's easier to identify when sending. Second step, this time click on this large folder and then right-click. Select &quot;Add to archive&quot;. Step 3: At this time, the software will automatically help us package our files. Select &quot;Compress to XX.zip&quot;. This zip is the packaging format, and then click Compress Now.​

winrar 64-bit-How to decompress winrar? winrar 64-bit-How to decompress winrar? Mar 18, 2024 pm 12:55 PM

WinRAR is a powerful compressed file management tool that provides rich features and an easy-to-use interface. WinRAR 64-bit version is specially optimized for 64-bit operating systems and can better utilize system resources and performance. Next, let the editor introduce you to winrar 64-bit and explain how to decompress winrar! 1. What is winrar 64-bit software? WinRAR is a powerful compressed package manager. This software can be used to back up your data, reduce the size of email attachments, decompress RAR, ZIP and other files downloaded from the Internet, and create new files in RAR and ZIP formats. The latest WINRAR version is Wi

How to correctly understand the differences between packaging and compression in Linux How to correctly understand the differences between packaging and compression in Linux Feb 20, 2024 pm 05:33 PM

Packaging and compression are commonly used operations in Linux, but many users tend to confuse the two concepts. This article will discuss the differences between packaging and compression in Linux systems in detail, and use specific code examples to help readers better understand. First, we need to clarify the difference between packaging and compression. Packaging is the combination of multiple files or directories into a single file, often used to organize, archive, or transfer files. Compression is to compress one or more files through an algorithm to reduce the size of the file, save storage space or speed up the transmission.

Centos 7 compression and decompression command overview Centos 7 compression and decompression command overview Dec 31, 2023 pm 09:02 PM

1.Zip file compression syntax: zip plus the compressed file name plus the file name to be compressed. [root@localhost~]#ziptest.ziptest.txtadding:test.txt(deflated100%)[root@localhost~]#The percentage number of the progress bar appearing after it indicates that it has been compressed. Then you can verify it in ll to see if it has your name. file package. [root@localhost~]#llTotal usage 820004-rw-------.1rootroot1587 March 2216:58anaconda-ks.cfg-rw-r--r--.1ro

uniapp implements how to use the image cropping and compression library to implement image processing functions uniapp implements how to use the image cropping and compression library to implement image processing functions Oct 20, 2023 am 11:40 AM

uniapp implements how to use the image cropping and compression library to implement image processing functions. When developing mobile applications, image processing requirements are often involved, such as image cropping and compression. In response to these needs, uniapp provides a wealth of plug-ins and components, allowing developers to easily implement image processing functions. This article will introduce how to use the image cropping and compression library in uniapp to implement image processing functions, and provide corresponding code examples. Image cropping Image cropping refers to cutting out part of the image as needed.

Realme GT Neo6 is scheduled to be released on May 9th! The first AI digital human conference in the computer industry Realme GT Neo6 is scheduled to be released on May 9th! The first AI digital human conference in the computer industry May 08, 2024 pm 12:49 PM

On May 7, our mobile phone manufacturer officially announced that our company’s GTNeo6 launch conference is scheduled for May 9. GTNoe6 is positioned as a "performance storm", aiming to stir up the mid-range machine situation. In addition, this conference will also be the first AI digital human conference in the mobile phone industry. At that time, Realme Vice President, Global Marketing President, and China President Xu Qi will appear at the press conference in the form of a digital human. Digital man Xu Qi According to the official introduction, Realme GTNoe6, codenamed "Hurricane", is faster and stronger. It will challenge the strongest third-generation Snapdragon 8s flagship and the strongest product in its class. Recently, the Realme GTNeo6 was found to be directly on the e-commerce platform. Some core configurations were exposed, showing that the machine is not only equipped with a Snapdragon 8s processor, but also supports 120W flash charging.

See all articles