Home Backend Development PHP Tutorial String and multi-digit conversion functions in PHP

String and multi-digit conversion functions in PHP

Jul 29, 2016 am 09:00 AM
array byte quot

Conversion function

/**
 * [字符串转换为(2,8,16进制)ASCII码]
 * @param  string  $str     [待处理字符串]
 * @param  boolean $encode  [字符串转换为ASCII|ASCII转换为字符串]
 * @param  string  $intType [2,8,16进制标示]
 * @return string  byte_str [处理结果]
 * @author alexander
 */
function strtoascii($str, $encode=true, $intType="2"){
    if($encode == true){
        $byte_array = str_split($str);
        foreach($byte_array as &$value){
            $value = ord($value);
            switch ($intType) {
                case 16:
                    $value = sprintf("%02x", $value);
                    break;
                case 8:
                    $value = sprintf("%03o", $value);
                    break;
                default:
                    $value = sprintf("%08b", $value);
                    break;
            }
        }
        unset($value);
        $byte_str = implode('', $byte_array);
    }
    else{
        $chunk_size = $intType == 16 ? 2 : ($intType == 8 ? 3 : 8);
        $byte_array = chunk_split($str, $chunk_size);
        $byte_array = array_filter(explode("\r\n", $byte_array));
        foreach($byte_array as &$value){
            $fun_name = $intType == 16 ? 'hexdec' : ($intType == 8 ? 'octdec' : 'bindec');
            $value = $fun_name($value);
            $value = chr($value);
        }
        unset($value);
        $byte_str = implode('', $byte_array);
    }
    return $byte_str;
}
Copy after login

Multi-base in PHP

PHP Integer values ​​can be represented in decimal, hexadecimal, octal or binary, and can be preceded by an optional sign (- or +).

Binary: [+-]?0b[01]+

Octal: [+-]?0[1-7]+

Decimal: [+-]?[1-9][0-9]* |0

Hex: [+-]?[xX][0-9a-fA-F]+

Multiple base conversion function:

bindec Binary to decimal conversion
decbin Convert decimal to binary
octdec Convert octal to decimal
decoct Convert decimal to octal
hexdec hexdec Convert system to decimal
dechex Convert decimal to hexadecimal

The above introduces the string and multi-base conversion functions in PHP, including the relevant 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
4 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)

Golang function byte, rune and string type conversion skills Golang function byte, rune and string type conversion skills May 17, 2023 am 08:21 AM

In Golang programming, byte, rune and string types are very basic and common data types. They play an important role in processing data operations such as strings and file streams. When performing these data operations, we usually need to convert them to each other, which requires mastering some conversion skills. This article will introduce the byte, rune and string type conversion techniques of Golang functions, aiming to help readers better understand these data types and be able to apply them skillfully in programming practice.

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

1byte equals how many bits 1byte equals how many bits Mar 19, 2021 pm 02:52 PM

1byte equals 8bit. Data storage is in "byte" (Byte) as the unit, and data transmission is mostly in "bit" (bit) as the unit. One bit represents a 0 or 1 (that is, binary), and every 8 bits (bit) form a Byte is the smallest unit of information; therefore, "1Byte=8bit".

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Simple and clear method to use PHP array_merge_recursive() function Simple and clear method to use PHP array_merge_recursive() function Jun 27, 2023 pm 01:48 PM

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values ​​of the same keys, making the program design more flexible. array_merge

How to use the array_combine function in PHP to combine two arrays into an associative array How to use the array_combine function in PHP to combine two arrays into an associative array Jun 26, 2023 pm 01:41 PM

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values ​​of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

What are the methods of converting java Object to byte and byte to Object? What are the methods of converting java Object to byte and byte to Object? Apr 20, 2023 am 11:37 AM

Object to byte and byte to Object Today we will realize how to convert from Object to byte and how to convert from byte to Object. First, define a class student: packagecom.byteToObject;importjava.io.Serializable;publicclassstudentimplementsSerializable{privateintsid;privateStringname;publicintgetSid(){returnsid;}publicvoidsetSid(in

Detailed explanation of PHP array_fill() function usage Detailed explanation of PHP array_fill() function usage Jun 27, 2023 am 08:42 AM

In PHP programming, array is a very important data structure that can handle large amounts of data easily. PHP provides many array-related functions, array_fill() is one of them. This article will introduce in detail the usage of the array_fill() function, as well as some tips in practical applications. 1. Overview of the array_fill() function The function of the array_fill() function is to create an array of a specified length and composed of the same values. Specifically, the syntax of this function is

See all articles