How to Convert String to Byte Array in PowerShell?

Do you ever need to convert string to byte array in PowerShell? In this PowerShell tutorial, I will explain how to convert strings to byte arrays in PowerShell using different methods with examples.

To convert a string to a byte array in PowerShell, use the System.Text.Encoding class with the appropriate encoding method. For example, $byteArray = [System.Text.Encoding]::UTF8.GetBytes($string) will convert $string into a UTF-8 encoded byte array. If you need a different encoding, replace UTF8 with ASCII, Unicode, or another supported encoding type as needed.

What are Byte Arrays in PowerShell

Let us first understand what a byte array is. A byte array is a collection of bytes that can represent any binary data. A single byte can hold a value from 0 to 255, which is the range of an unsigned 8-bit integer. In the context of strings, each character can be represented by one or more bytes, depending on the encoding used.

Once you have a byte array, you can manipulate it as needed for your application. For example, you might want to convert it back to a string or write it to a file. Here’s how you would convert a byte array back to a string using UTF-8 encoding:

# Assume $byteArray contains the byte array you want to convert back to a string

# Create an instance of the UTF8 encoding
$utf8Encoding = [System.Text.Encoding]::UTF8

# Convert the byte array back to a string
$string = $utf8Encoding.GetString($byteArray)

# Output the string
$string

And here’s how you might write a byte array to a file:

# Assume $byteArray contains the byte array you want to write to a file
# Define the path to the output file
$outputFilePath = "C:\path\to\output\file.bin"

# Write the byte array to the file
[System.IO.File]::WriteAllBytes($outputFilePath, $byteArray)

How to Convert String to Byte Array in PowerShell

Now, let us see how to convert string to byte array in PowerShell using various methods.

Method 1: Using the Encoding Class

The most common way to convert a string to a byte array in PowerShell is by using the System.Text.Encoding class. This class provides several methods for encoding strings into bytes and vice versa. Here’s an example using UTF-8 encoding:

# Define the string to convert
$stringToConvert = "This is a sample string"

# Create an instance of the UTF8 encoding
$utf8Encoding = [System.Text.Encoding]::UTF8

# Convert the string to a byte array
$byteArray = $utf8Encoding.GetBytes($stringToConvert)

# Output the byte array
$byteArray

In this example, the GetBytes method of the UTF8 encoding class is used to convert the string into a byte array. UTF-8 is a common encoding that supports all Unicode characters.

I executed the PowerShell script using Visual Studio code, check out the screenshot below for reference.

Convert String to Byte Array in PowerShell

Method 2: Using Base64 Conversion

Another method to convert a string to a byte array involves using Base64 encoding in PowerShell. This method is particularly useful when you need to encode binary data to a string format that can be easily transmitted or stored. Here’s how you can achieve this in PowerShell:

# Define the string to convert
$stringToConvert = "This is a sample string"

# Convert the string to a Base64 string
$base64String = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($stringToConvert))

# Now convert the Base64 string back to a byte array
$byteArray = [Convert]::FromBase64String($base64String)

# Output the byte array
$byteArray

In the above code, the ToBase64String method is used to convert the byte array obtained from the string into a Base64 string. Then, the FromBase64String method converts the Base64 string back to the original byte array.

Method 3: Using ASCII Encoding

If you’re dealing with only ASCII characters, you can use the ASCII encoding to convert the string to a byte array in PowerShell. Here’s an example:

# Define the string to convert
$stringToConvert = "This is a sample string"

# Create an instance of the ASCII encoding
$asciiEncoding = [System.Text.Encoding]::ASCII

# Convert the string to a byte array
$byteArray = $asciiEncoding.GetBytes($stringToConvert)

# Output the byte array
$byteArray

The ASCII encoding will represent each character as a single byte, which is suitable for characters in the standard ASCII character set.

Conclusion

You can easily convert strings to byte arrays in PowerShell using various methods provided by the System.Text.Encoding class. Whether you’re using UTF-8, Base64, or ASCII encoding, the process involves just a few lines of code.

In this PowerShell tutorial, I have explained how to convert string to byte array in PowerShell using various methods and examples.

You may also like: