In program development, data format conversion is an essential part. In Java and PHP, for array type data, we often need to convert it to string type. Among them, byte array is one of the common array types. Let's discuss how to convert byte array to string in PHP and Java.
1. Convert byte array to string in Java
Java provides multiple ways to convert byte array into string. Here are only two commonly used methods.
The String class in Java provides a constructor that supports converting byte arrays into strings. By calling the new String(byte[]) method and passing the byte array as a parameter, the byte array to string operation can be realized.
The sample code is as follows:
byte[] byteArray = new byte[]{104, 101, 108, 108, 111}; // hello的ASCII码 String str = new String(byteArray); System.out.println(str); // 输出:hello
In addition to using the constructor of the String class, Base64 encoding can also be used in Java Convert byte array to string. Base64 is an encoding method that converts binary data into readable ASCII strings. It can encode binary data of any length into a fixed-length string.
Using Base64 encoding in Java requires the help of the java.util.Base64 class in the Java standard library. By calling the encodeToString method of this class and passing in the byte array as a parameter, the byte array to string operation can be implemented.
The sample code is as follows:
byte[] byteArray = new byte[]{104, 101, 108, 108, 111}; // hello的ASCII码 String str = java.util.Base64.getEncoder().encodeToString(byteArray); System.out.println(str); // 输出:aGVsbG8=
2. Convert byte array to string in PHP
Similar to Java, PHP also provides a variety of ways to convert byte arrays into characters String, here we mainly introduce two methods.
Using the for loop statement in PHP can extract each element of the byte array in turn and convert it into the corresponding character. These characters can be concatenated to obtain a string.
The sample code is as follows:
$byteArray = array(104, 101, 108, 108, 111); // hello的ASCII码 $str = ""; for ($i = 0; $i < count($byteArray); $i++) { $str .= chr($byteArray[$i]); } echo $str; // 输出:hello
The pack function in PHP can convert binary data into any encoding format, including characters string. By calling the pack function, we can convert the byte array into binary data, and then convert the binary data into a string.
The sample code is as follows:
$byteArray = array(104, 101, 108, 108, 111); // hello的ASCII码 $binString = call_user_func_array("pack", array_merge(array("C*"), $byteArray)); $str = $binString; echo $str; // 输出:hello
Summary
Whether in Java or PHP, we can convert a byte array into a string in many ways. The specific method used depends on the development environment and specific needs we are in. In the actual development process, it needs to be flexibly selected based on specific scenarios.
The above is the detailed content of How to convert php java byte array to string. For more information, please follow other related articles on the PHP Chinese website!