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

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

HWID V 2.2 manual activation methods and tutorials

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

Flexible application skills of position attribute in H5

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

Golang function byte, rune and string type conversion skills

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 put div at the bottom in html

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

How to solve 'undefined: bytes.Split' error in golang?

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

1byte equals how many bits

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 property optimization tips: position sticky and flexbox

What attributes does position have? What attributes does position have? Oct 10, 2023 am 11:18 AM

What attributes does position have?

See all articles