PHP implementation of byte array conversion in Java
Because of the work required, I wrote a PHP class that implements a byte array conversion for Java.
/**
? * byte array and string conversion class
? * @author Zikie
? * Created on 2011-7-15
?*/
Class Bytes {
???
??? /**
???? * Convert a string string to a byte array
???? * @param $str strings that need to be converted
???? * @param $bytes Target byte array
???? * @author Zikie
???? */
????
??? public static function GetBytes ($STR) {
??? ??? $len = strlen ($STR);
??? ??? $bytes = Array ();
?? ??? ??? for ($i =0; $i < $len; $i + +) {
?? ??? ??? ??? if (Ord ($str [$i]) >= 128) {
?? ??? ??? ??? ??? $byte = Ord ($str [$i])-256;
?? ??? ??? ??? }else{
?? ??? ??? ??? ??? $byte = Ord ($str [$i]);
?? ??? ??? ??? }
??????? ??? $bytes [] =? $byte;
??? ??? }
??? ??? return $bytes;
??? }
???
??? /**
???? * Converts a byte array to a string type of data
???? * @param $bytes byte array
???? * @param $str Target string
???? * @return A String type of data
???? */
????
??? public static function Tostr ($bytes) {
??? ??? $str = ";
??? ??? foreach ($bytes as $ch) {
??????? ??? $str. = Chr ($ch);
??? ??? }
?? ??? ??? return $str;
??? }
???
??? /**
???? * Convert an int to a byte array
???? * @param $byt Target byte array
???? * @param $val strings that need to be converted
???? * @author Zikie
???? */
???
??? public static function Integertobytes ($val) {
??? ??? $byt = Array ();
??? ??? $byt [0] = ($val & 0xff);
??? ??? $BYT [1] = ($val >> 8 & 0xff);
??? ??? $BYT [2] = ($val >> & 0xff);
??????? $BYT [3] = ($val >> & 0xff);
??????? return $byt;
??? }
???
??? /**
???? * Reads an integer type of data from the location specified in the byte array
???? * @param $bytes byte array
???? * @param $position The starting position specified
???? * @return An integer type of data
???? */
????
??? public static function Bytestointeger ($bytes, $position) {
??????? $val = 0;
??????? $val = $bytes [$position + 3] & 0xFF;
??????? $val <<= 8;
??????? $val |= $bytes [$position + 2] & 0xFF;
??????? $val <<= 8;
??????? $val |= $bytes [$position + 1] & 0xFF;
??????? $val <<= 8;
??????? $val |= $bytes [$position] & 0xFF;
??????? return $val;
??? }
??? /**
???? * Convert a Shor string to a byte array
???? * @param $byt Target byte array
???? * @param $val strings that need to be converted
???? * @author Zikie
???? */
???
??? public static function Shorttobytes ($val) {
??? ??? $byt = Array ();
??? ??? $byt [0] = ($val & 0xff);
??????? $BYT [1] = ($val >> 8 & 0xff);
??????? return $byt;
??? }
???
??? /**
???? * Reads a short type of data from the location specified in the byte array.
???? * @param $bytes byte array
???? * @param $position The starting position specified
???? * @return A short type of data
???? */
????
??? public static function Bytestoshort ($bytes, $position) {
??????? $val = 0;
??????? $val = $bytes [$position + 1] & 0xFF;
??????? $val = $val << 8;
??????? $val |= $bytes [$position] & 0xFF;
??????? return $val;
??? }
???
}
?>
1/F Tron.lu 2012-02-06
Do you use 3DES encryption?
2/F heaven__18 2012-03-13
It can be used in many places.