How To Convert Byte Array To String In PowerShell?

In this PowerShell tutorial, I will explain how to convert byte array to string in PowerShell using various methods.

To convert a byte array to a string in PowerShell, use the System.Text.Encoding class with the GetString method. For instance, $string = [System.Text.Encoding]::UTF8.GetString($byteArray) will convert $byteArray into a UTF-8 encoded string. Adjust the encoding as necessary to match the byte array’s encoding.

1. Using System.Text.Encoding

The most straightforward method to convert a byte array to a string in PowerShell is by using the System.Text.Encoding class. This class provides methods for encoding a set of characters into a sequence of bytes and for decoding a sequence of bytes into a set of characters. Here’s how you can use it:

# Create a byte array
$byteArray = [System.Text.Encoding]::UTF8.GetBytes("Hello, World!")

# Convert byte array to string
$string = [System.Text.Encoding]::UTF8.GetString($byteArray)

# Display the string
$string

In this example, we first use the GetBytes method of the UTF8 encoding to convert a string to a byte array. Then, we use the GetString method to convert the byte array back to a string. The UTF-8 encoding is commonly used because it supports all Unicode characters and is backward compatible with ASCII.

You can see the output in the screenshot below after I executed the script using VS Code.

Convert Byte Array To String In PowerShell

2. Using Base64 Encoding

Another way to represent binary data as a string is by using Base64 encoding in PowerShell. This method is useful when you need to safely transmit binary data over a medium that only supports text. Here’s how you can convert a byte array to a Base64 encoded string:

# Create a byte array
$byteArray = [System.Text.Encoding]::UTF8.GetBytes("Hello, World!")

# Convert byte array to Base64 string
$base64String = [System.Convert]::ToBase64String($byteArray)

# Display the Base64 string
$base64String

To convert the Base64 string back to a byte array, you can use the FromBase64String method:

# Convert Base64 string to byte array
$byteArrayFromBase64 = [System.Convert]::FromBase64String($base64String)

# Convert byte array to string to verify the result
$stringFromBase64 = [System.Text.Encoding]::UTF8.GetString($byteArrayFromBase64)

# Display the string
$stringFromBase64

3. Using a StringBuilder and BitConverter

If you need to convert a byte array to a hexadecimal string representation, you can use the BitConverter class combined with a StringBuilder. Here’s an example:

# Create a byte array
$byteArray = [System.Text.Encoding]::UTF8.GetBytes("Hello, World!")

# Use BitConverter to get a hexadecimal string
$hexString = [System.BitConverter]::ToString($byteArray)

# Display the hexadecimal string
$hexString

The ToString method of the BitConverter class will return a string with the hexadecimal values separated by hyphens. If you want to remove the hyphens, you can use the Replace method:

# Remove hyphens from the hexadecimal string
$hexString = $hexString.Replace("-", "")

# Display the cleaned-up hexadecimal string
$hexString

When converting byte arrays to strings, it’s crucial to understand the concept of encoding. Encoding is the process of transforming a set of Unicode characters into a sequence of bytes. Decoding is the reverse process, converting a sequence of bytes back into a set of characters. The choice of encoding (e.g., UTF-8, ASCII, Unicode) affects how the bytes are interpreted and must be consistent during both encoding and decoding to avoid data corruption.

Conclusion

PowerShell provides flexible methods for converting byte arrays to strings, whether you need a plain text representation, a Base64 encoded string, or a hexadecimal string.

In this PowerShell tutorial, I have explained how to convert byte array to string in PowerShell.

You may also like: