Home > Java > javaTutorial > How to Convert a Java Byte Array to a String and Back?

How to Convert a Java Byte Array to a String and Back?

Mary-Kate Olsen
Release: 2024-11-10 03:36:02
Original
1061 people have browsed it

How to Convert a Java Byte Array to a String and Back?

Java Byte Array to String and Back to Byte Array

In Java, converting a byte array to a string is a common task. However, it's crucial to understand that this involves two operations: converting a byte array to a string representation and then back to a byte array. This article aims to clarify this process.

Conversion from Byte Array to String

The Arrays.toString() method is commonly used to obtain a string representation of a byte array. For example, if we have a byte array data containing values [-47, 1, 16, 84, 2, 101, 110, 83, 111, 109, 101, 32, 78, 70, 67, 32, 68, 97, 116, 97], calling Arrays.toString(data.toByteArray()) would yield:

[-47, 1, 16, 84, 2, 101, 110, 83, 111, 109, 101, 32, 78, 70, 67, 32, 68, 97, 116, 97]
Copy after login
Copy after login

This string representation can be sent over a network or stored in a database.

Conversion from String Back to Byte Array

When receiving the string representation on the receiving end, it's necessary to convert it back into a byte array. This involves parsing the string and reconstructing the byte array.

One way to approach this is to use the byteValues() method, which takes a string like "[-47, 1, 16, 84, 2, 101, 110, 83, 111, 109, 101, 32, 78, 70, 67, 32, 68, 97, 116, 97]" and returns an array of strings:

[-47, 1, 16, 84, 2, 101, 110, 83, 111, 109, 101, 32, 78, 70, 67, 32, 68, 97, 116, 97]
Copy after login
Copy after login

Each element in this array represents a byte value. To convert this back to a byte array, you can use a loop to iterate over the strings, parse them to bytes, and store them in a new byte array.

Example Code

The following Python script illustrates the server-side handling of the data received from the client:

response = "[-47, 1, 16, 84, 2, 101, 110, 83, 111, 109, 101, 32, 78, 70, 67, 32, 68, 97, 116, 97]"      # Placeholder response from the client

byte_values = response.substring(1, response.length() - 1).split(",")
# Convert the string values to bytes
bytes = bytearray([int(byte_value.strip()) for byte_value in byte_values])

# Send the bytes back to the client
Copy after login

Note:

Remember that when Arrays.toString() converts a byte array to a string, it loses the original data type information. Therefore, to correctly convert the string back to a byte array, you need to parse the string carefully and manually reconstruct the byte array using the appropriate methods.

The above is the detailed content of How to Convert a Java Byte Array to a String and Back?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template