C# / .NET Tutorial

Letters to Numbers in C# — Complete Guide with Code

AlphaCoder Team|May 23, 2026|10 min read

C# makes letter-to-number conversion elegant with its strong type system, LINQ queries, and extension methods. Whether you are building a cipher tool, validating user input, or processing text data in .NET, converting between letters and numbers is straightforward. For instant results without writing code, use our letters to numbers converter online.

Basic char Arithmetic in C#

In C#, char is an integral type. You can perform arithmetic directly on character values, just like integers. This is the foundation for all letter-number conversions.

// Letter to alphabet position (A=1, Z=26)
char letter = 'M';
int position = char.ToUpper(letter) - 'A' + 1;  // 13

// Position to letter
int pos = 13;
char result = (char)('A' + pos - 1);  // 'M'

// ASCII value
int asciiValue = (int)'A';  // 65
int asciiLower = (int)'a';  // 97

A Complete Converter Class

public static class AlphabetConverter
{
    /// <summary>Convert a letter to its position (A=1, Z=26)</summary>
    public static int ToPosition(char c)
    {
        if (!char.IsLetter(c))
            throw new ArgumentException($"'{c}' is not a letter");
        return char.ToUpper(c) - 'A' + 1;
    }

    /// <summary>Convert a position (1-26) to uppercase letter</summary>
    public static char ToLetter(int position)
    {
        if (position < 1 || position > 26)
            throw new ArgumentOutOfRangeException(
                nameof(position), "Must be 1-26");
        return (char)('A' + position - 1);
    }

    /// <summary>Encode text as A1Z26 numbers</summary>
    public static string Encode(string text, string separator = "-")
    {
        return string.Join(separator,
            text.ToUpper()
                .Where(char.IsLetter)
                .Select(c => (c - 'A' + 1).ToString()));
    }

    /// <summary>Decode A1Z26 numbers back to text</summary>
    public static string Decode(string encoded, string separator = "-")
    {
        return new string(
            encoded.Split(separator)
                   .Where(s => int.TryParse(s.Trim(), out _))
                   .Select(s => int.Parse(s.Trim()))
                   .Where(n => n >= 1 && n <= 26)
                   .Select(n => (char)('A' + n - 1))
                   .ToArray());
    }

    /// <summary>Calculate the sum of letter positions</summary>
    public static int WordValue(string text)
    {
        return text.ToUpper()
                   .Where(char.IsLetter)
                   .Sum(c => c - 'A' + 1);
    }
}

This class covers the core operations: single letter conversion, full string encoding/decoding, and word value calculation. The same logic powers our word value calculator.

LINQ Approach for String Processing

LINQ is C#'s most expressive tool for data transformation. Here are several LINQ patterns for letter-number work:

using System.Linq;

string text = "Hello World";

// Get array of positions
int[] positions = text.ToUpper()
    .Where(char.IsLetter)
    .Select(c => c - 'A' + 1)
    .ToArray();
// [8, 5, 12, 12, 15, 23, 15, 18, 12, 4]

// Get positions as formatted string
string encoded = string.Join("-", positions);
// "8-5-12-12-15-23-15-18-12-4"

// Word-by-word encoding
string wordEncoded = string.Join(" / ",
    text.Split(' ')
        .Select(word => string.Join("-",
            word.ToUpper()
                .Where(char.IsLetter)
                .Select(c => c - 'A' + 1))));
// "8-5-12-12-15 / 23-15-18-12-4"

// ASCII values instead of positions
int[] asciiValues = text.Select(c => (int)c).ToArray();
// [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]

Extension Methods for Clean Code

Extension methods let you add conversion abilities directly to char and string types, resulting in highly readable code:

public static class AlphabetExtensions
{
    // Char extensions
    public static int ToAlphabetPosition(this char c)
        => char.IsLetter(c) ? char.ToUpper(c) - 'A' + 1 : -1;

    public static int ToAsciiValue(this char c)
        => (int)c;

    // Int extensions
    public static char ToLetter(this int position)
        => position >= 1 && position <= 26
            ? (char)('A' + position - 1)
            : throw new ArgumentOutOfRangeException();

    // String extensions
    public static string ToA1Z26(this string text, string sep = "-")
        => string.Join(sep,
            text.ToUpper().Where(char.IsLetter)
                .Select(c => (c - 'A' + 1).ToString()));

    public static int ToWordValue(this string text)
        => text.ToUpper().Where(char.IsLetter).Sum(c => c - 'A' + 1);
}

// Usage:
// 'M'.ToAlphabetPosition()      → 13
// 13.ToLetter()                 → 'M'
// "Hello".ToA1Z26()             → "8-5-12-12-15"
// "CSHARP".ToWordValue()        → 70

Working with ASCII Values

The ASCII converter page shows all 128 ASCII values. In C#, getting ASCII values is trivial because char is already numeric:

// Cast to int for ASCII value
int ascii = (int)'A';  // 65

// Full ASCII table for A-Z
for (char c = 'A'; c <= 'Z'; c++)
{
    Console.WriteLine(
        $"{c} = Position {c - 'A' + 1}, " +
        $"ASCII {(int)c}, " +
        $"Hex 0x{(int)c:X2}, " +
        $"Binary {Convert.ToString(c, 2).PadLeft(8, '0')}");
}
// A = Position 1,  ASCII 65, Hex 0x41, Binary 01000001
// B = Position 2,  ASCII 66, Hex 0x42, Binary 01000010
// ... through Z

Handling Unicode with C#

C# strings are UTF-16 internally. For characters outside the Basic Multilingual Plane, use char.ConvertToUtf32():

// Standard English letters work with basic char
char letter = 'Z';
int position = letter - 'A' + 1;  // 26

// For accented characters, normalize first
string accented = "cafe\u0301";  // café
string normalized = accented.Normalize(NormalizationForm.FormC);
// Then filter to basic A-Z for position calculation

// Check if a char is basic ASCII letter
bool isAsciiLetter = c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z';

Real-World Example: Processing CSV Data

// Convert a column of names to their word values
var lines = File.ReadAllLines("names.csv");
var results = lines.Skip(1)  // Skip header
    .Select(line =>
    {
        var name = line.Split(',')[0].Trim();
        var value = name.ToUpper()
            .Where(char.IsLetter)
            .Sum(c => c - 'A' + 1);
        return new { Name = name, Value = value };
    })
    .OrderByDescending(x => x.Value);

foreach (var r in results)
    Console.WriteLine($"{r.Name}: {r.Value}");

Unit Testing with xUnit

public class AlphabetConverterTests
{
    [Theory]
    [InlineData('A', 1)]
    [InlineData('Z', 26)]
    [InlineData('M', 13)]
    [InlineData('a', 1)]   // lowercase
    public void ToPosition_ReturnsCorrectValue(char input, int expected)
    {
        Assert.Equal(expected, AlphabetConverter.ToPosition(input));
    }

    [Theory]
    [InlineData(1, 'A')]
    [InlineData(26, 'Z')]
    [InlineData(13, 'M')]
    public void ToLetter_ReturnsCorrectChar(int input, char expected)
    {
        Assert.Equal(expected, AlphabetConverter.ToLetter(input));
    }

    [Fact]
    public void Encode_HandlesFullString()
    {
        Assert.Equal("8-5-12-12-15",
            AlphabetConverter.Encode("Hello"));
    }

    [Fact]
    public void RoundTrip_PreservesText()
    {
        string original = "CSHARP";
        string encoded = AlphabetConverter.Encode(original);
        string decoded = AlphabetConverter.Decode(encoded);
        Assert.Equal(original, decoded);
    }

    [Fact]
    public void WordValue_CalculatesCorrectly()
    {
        Assert.Equal(52, AlphabetConverter.WordValue("Hello"));
    }
}

Try it instantly: Convert any word to numbers using our ASCII Converter or calculate word sums with the Word Value Calculator.

Try it now →

Frequently Asked Questions

How do I convert a letter to its alphabet position in C#?

Use char.ToUpper(letter) - 'A' + 1 for a one-based position (A=1, Z=26). C# chars are implicitly numeric, so you can subtract them directly. Validate with char.IsLetter(c) first to handle non-letter input safely.

How do I use LINQ to convert an entire string to numbers in C#?

Chain .ToUpper().Where(char.IsLetter).Select(c => c - 'A' + 1) on the string. LINQ's fluent syntax processes each character in sequence. Use .ToArray() for an int array or string.Join() for formatted output.

Can I create an extension method for letter-to-number conversion in C#?

Yes, add a static method in a static class with the this keyword on the first parameter. For example, public static int ToAlphabetPosition(this char c) lets you write 'M'.ToAlphabetPosition() which returns 13. Extension methods are resolved at compile time with zero runtime overhead.

Related Articles