How to Convert Char to Int using PowerShell

Characters are stored internally as numeric codes—usually Unicode or ASCII values—and sometimes you need these numbers. For example, you might need to convert the letter 'A' into its numeric value 65 when preparing data for encryption or checking character ranges.

In this tutorial, I explain multiple methods for converting characters into integers in PowerShell with real examples.

Characters and Integers in PowerShell

In PowerShell, a char is a single Unicode character, while an int represents a whole number. Even a single character you type is often stored as a string until you explicitly cast it.

Here’s an example:

$char = 'A'
$char.GetType().Name

Output:

String

Here is the exact output in the screenshot below:

PowerShell Convert Char to Int

That’s because 'A' without explicit casting is treated as a string, even though it contains only one character. Behind the scenes, every character has a numeric code that identifies it; with proper conversion, you can retrieve that value.

Check out PowerShell Print Variable

Method 1: Casting with [int][char]

PowerShell allows type casting using the syntax [type]value. When converting a character to an integer, the most direct method is:

[int][char]'A'

Output:

65

How it works:

  1. [char]'A' converts the string 'A' to a System.Char type.
  2. [int] then converts that character into its Unicode numeric code.

You can apply this to multiple characters:

$chars = 'A','B','C'
$chars | ForEach-Object { [int][char]$_ }

Output:

65
66
67

You can check the exact output in the screenshot below:

PowerShell Convert Char to Int examples

This method is concise, works for both ASCII and Unicode characters, and is the go-to for most scripts.

Check out Concatenate String and Integer in PowerShell

Method 2: Casting with [byte][char]

If your goal is to stick within the ASCII range (0–255), you can cast to byte instead:

[byte][char]'a'

Output:

97

This method is useful when working with ASCII-only data, such as simple English text or protocols that do not require Unicode support. Be cautious—casting extended Unicode characters to byte may not return meaningful results or could throw errors.

Check out PowerShell Round to Nearest Whole Number

Method 3: Using .ToCharArray() and Casting

When processing entire strings, .ToCharArray() is an excellent tool. It splits your string into individual characters, letting you process each one:

$string = "Hello"
foreach ($ch in $string.ToCharArray()) {
    $code = [int][char]$ch
    Write-Output "$ch = $code"
}

Output:

H = 72
e = 101
l = 108
l = 108
o = 111

Here is the exact output in the screenshot below:

Convert Char to Int using PowerShell

This method is perfect for tasks like encoding text, analyzing strings, or building arrays of numeric codes for storage and processing.

Read How to Get the Type of an Object in PowerShell?

Method 4: Using [System.Text.Encoding]

PowerShell can access .NET classes for more complex encoding operations. One example is System.Text.Encoding, which returns byte arrays for characters based on specific encodings.

$char = 'A'
[System.Text.Encoding]::UTF8.GetBytes($char)

Output:

65

Method 5: Using [Convert]::ToInt32()

PowerShell also supports the .NET Convert class for type conversion. With it, you can make your intention clear to other developers:

[Convert]::ToInt32([char]'Z')

Output:

90

While not as compact as direct casting, this improves readability in scripts where clarity is more important than brevity.

Convert a String to ASCII Codes Example

Here’s a useful script that takes a string and converts each character to its ASCII code:

$text = "PowerShell"
$asciiCodes = $text.ToCharArray() | ForEach-Object { [int][char]$_ }
Write-Output $asciiCodes

Output:

80 111 119 101 114 83 104 101 108 108

This can be applied to encryption, encoding, or even simple validation scripts where character codes are checked against a rule set.

Check out Concatenate Strings and Floats in PowerShell

Best Practices

  • Use [int][char] when you need a fast, clean conversion.
  • Use .ToCharArray() for processing strings character-by-character.
  • Use System.Text.Encoding when working within specific encoding formats.
  • Always test scripts with Unicode input if you expect multilingual data.

Common Mistakes to Avoid

  1. Forgetting to cast twice:
[int]'A'  # Error
[int][char]'A'  # Correct
  1. Without the char cast, PowerShell won’t know how to interpret 'A' as an int.
  2. Unicode vs ASCII confusion:
    Using [byte][char] for non-ASCII characters may lead to data loss. Stick with [int][char] or encoding methods for Unicode data.
  3. Not checking data types:
    Always verify data types:
$val = [int][char]'A'
$val.GetType().Name

Conclusion

You’ve learned five different ways to achieve this, along with cases where each method excels. Whether you’re encoding data, processing text, or manipulating characters for logic, these techniques will make your scripts more powerful and flexible. In this tutorial, I explained how to convert a Char to an Int in PowerShell.

You may also like:

Leave a Comment

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.