php兑现java的byte数组转换
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;
??? }
???
}
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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.

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.

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

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.
