Home Backend Development PHP Tutorial php兑现java的byte数组转换

php兑现java的byte数组转换

Jun 13, 2016 am 10:54 AM
byte bytes param position val

php实现java的byte数组转换

由于工作需要,自己写的一个php实现java的byte数组转换的类。



/**
?* byte数组与字符串转化类
?* @author zikie
?* Created on 2011-7-15
?*/

class Bytes {
???
??? /**
???? * 转换一个String字符串为byte数组
???? * @param $str 需要转换的字符串
???? * @param $bytes 目标byte数组
???? * @author Zikie
???? */
????
??? public static function getBytes($str) {

??? ??? $len = strlen($str);
??? ??? $bytes = array();
?? ??? ??? for($i=0;$i?? ??? ??? ??? if(ord($str[$i]) >= 128){
?? ??? ??? ??? ??? $byte = ord($str[$i]) - 256;
?? ??? ??? ??? }else{
?? ??? ??? ??? ??? $byte = ord($str[$i]);
?? ??? ??? ??? }
??????? ??? $bytes[] =? $byte ;
??? ??? }
??? ??? return $bytes;
??? }
???
??? /**
???? * 将字节数组转化为String类型的数据
???? * @param $bytes 字节数组
???? * @param $str 目标字符串
???? * @return 一个String类型的数据
???? */
????
??? public static function toStr($bytes) {
??? ??? $str = '';
??? ??? foreach($bytes as $ch) {
??????? ??? $str .= chr($ch);
??? ??? }

?? ??? ??? return $str;
??? }
???
??? /**
???? * 转换一个int为byte数组
???? * @param $byt 目标byte数组
???? * @param $val 需要转换的字符串
???? * @author Zikie
???? */
???
??? public static function integerToBytes($val) {
??? ??? $byt = array();
??? ??? $byt[0] = ($val & 0xff);
??? ??? $byt[1] = ($val >> 8 & 0xff);
??? ??? $byt[2] = ($val >> 16 & 0xff);
??????? $byt[3] = ($val >> 24 & 0xff);
??????? return $byt;
??? }
???
??? /**
???? * 从字节数组中指定的位置读取一个Integer类型的数据
???? * @param $bytes 字节数组
???? * @param $position 指定的开始位置
???? * @return 一个Integer类型的数据
???? */
????
??? public static function bytesToInteger($bytes, $position) {
??????? $val = 0;
??????? $val = $bytes[$position + 3] & 0xff;
??????? $val ??????? $val |= $bytes[$position + 2] & 0xff;
??????? $val ??????? $val |= $bytes[$position + 1] & 0xff;
??????? $val ??????? $val |= $bytes[$position] & 0xff;
??????? return $val;
??? }

??? /**
???? * 转换一个shor字符串为byte数组
???? * @param $byt 目标byte数组
???? * @param $val 需要转换的字符串
???? * @author Zikie
???? */
???
??? public static function shortToBytes($val) {
??? ??? $byt = array();
??? ??? $byt[0] = ($val & 0xff);
??????? $byt[1] = ($val >> 8 & 0xff);
??????? return $byt;
??? }
???
??? /**
???? * 从字节数组中指定的位置读取一个Short类型的数据。
???? * @param $bytes 字节数组
???? * @param $position 指定的开始位置
???? * @return 一个Short类型的数据
???? */
????
??? public static function bytesToShort($bytes, $position) {
??????? $val = 0;
??????? $val = $bytes[$position + 1] & 0xFF;
??????? $val = $val ??????? $val |= $bytes[$position] & 0xFF;
??????? return $val;
??? }
???
}
?>

1 楼 tron.lu 2012-02-06  
3des 加密的时候用吗?

2 楼 heaven__18 2012-03-13  
在很多地方都能用的到。
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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

HWID V 2.2 manual activation methods and tutorials HWID V 2.2 manual activation methods and tutorials Oct 20, 2023 pm 07:17 PM

This is for users who want to perform manual activation. If you need help with a tool for you to do this, then check here. We can divide the manual activation process into two parts. 1- From the ready-made batch file make sure the internet is enabled. Open Windows Powershell as administrator and enter the following to list the commands in the order they are given. Enter the key, (replace with the key from the list above) Use the following command <key>slmgr/ipk<key> Download the universal ticket from here and extract the downloaded file. Now enter the following code in Powershell (Get-ItemProper

Flexible application skills of position attribute in H5 Flexible application skills of position attribute in H5 Dec 27, 2023 pm 01:05 PM

How to flexibly use the position attribute in H5. In H5 development, the positioning and layout of elements are often involved. At this time, the CSS position property will come into play. The position attribute can control the positioning of elements on the page, including relative positioning, absolute positioning, fixed positioning and sticky positioning. This article will introduce in detail how to flexibly use the position attribute in H5 development.

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.

How to solve 'undefined: bytes.Split' error in golang? How to solve 'undefined: bytes.Split' error in golang? Jun 25, 2023 pm 02:02 PM

In the Go language, the bytes package is a package for manipulating byte types, and it contains many useful methods, such as the Split() method. However, when using the Split() method, you may encounter an "undefined: bytes.Split" error. This error is usually caused by incompatible Go versions or lack of necessary dependent libraries. This article will introduce some methods to solve this error. Method 1: Upgrade the Go version as follows

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

How to put div at the bottom in html How to put div at the bottom in html Mar 02, 2021 pm 05:44 PM

How to place a div at the bottom of HTML: 1. Use the position attribute to position the div tag relative to the browser window, with the syntax "div{position:fixed;}"; 2. Set the distance to the bottom to 0 to permanently place the div at At the bottom of the page, the syntax is "div{bottom:0;}".

CSS layout property optimization tips: position sticky and flexbox CSS layout property optimization tips: position sticky and flexbox Oct 20, 2023 pm 03:15 PM

CSS layout attribute optimization tips: positionsticky and flexbox In web development, layout is a very important aspect. A good layout structure can improve the user experience and make the page more beautiful and easy to navigate. CSS layout properties are the key to achieving this goal. In this article, I will introduce two commonly used CSS layout property optimization techniques: positionsticky and flexbox, and provide specific code examples. 1. positions

How to use position in h5 How to use position in h5 Dec 26, 2023 pm 01:39 PM

In H5, you can use the position attribute to control the positioning of elements through CSS: 1. Relative positioning, the syntax is "style="position: relative;"; 2. Absolute positioning, the syntax is "style="position: absolute;" "; 3. Fixed positioning, the syntax is "style="position: fixed;" and so on.

See all articles