C# Convert ToInt64() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Convert

Definition

In C#, Convert.ToInt64 returns a long (64-bit signed integer) from text or other numbers.

Use it when values may exceed the 32-bit int range. Base overloads parse binary, octal, or hex strings.

This is a static member of System.Convert. You call it with the type name, such as Convert.ToInt64(...). Convert is the everyday API for converting between common CLR types in C#.

Syntax

Convert text or numbers into a long.

C#
long a = Convert.ToInt64("3000000000");
long b = Convert.ToInt64(42);
long c = Convert.ToInt64("1A", 16);
CallWhat it does
Convert.ToInt64("3000000000")Large digit string → long.
Convert.ToInt64("1A", 16)Hex with base 16.

null → 0. Prefer long.TryParse for untrusted text.

Library declarations
C#
public static long ToInt64(string? value);
public static long ToInt64(string? value, int fromBase);
public static long ToInt64(int value);

Parameters

ParameterDescription
valueRequired. Text or number to convert.
fromBaseOptional. 2, 8, 10, or 16.

Return Value

Return ValueDescription
long64-bit signed result.

C# Examples

Example 1 — Beyond Int32 Range

Parse a value too big for a 32-bit int.

C#
using System;

class Program {
    static void Main() {
        long n = Convert.ToInt64("3000000000");
        Console.WriteLine(n);
        Console.WriteLine(n + 1);
    }
}

How It Works

  1. 3 billion. Larger than Int32.MaxValue.
  2. long fits. ToInt64 stores it safely.
  3. Increment. Adding 1 shows ordinary math.
  4. Choose width. Use Int64 when int is too small.

Example 2 — Binary String to Long

Parse a binary digit string.

C#
using System;

class Program {
    static void Main() {
        long n = Convert.ToInt64("11111111", 2);
        Console.WriteLine(n);
        Console.WriteLine(Convert.ToInt64("FF", 16));
    }
}

How It Works

  1. Eight ones. Binary 11111111 equals 255.
  2. Hex FF. Same magnitude from base 16.
  3. fromBase. Selects how digits are read.
  4. Result is long. Even small values use Int64 here.

Example 3 — Null Becomes Zero

Show Convert null behavior for ToInt64.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Convert.ToInt64(null));
        Console.WriteLine(Convert.ToInt64("0"));
        Console.WriteLine(Convert.ToInt64("-7"));
    }
}

How It Works

  1. Null path. Convert returns 0 instead of throwing.
  2. Zero text. "0" also yields 0.
  3. Negatives. Leading minus works for decimal strings.
  4. Unlike Parse. long.Parse(null) would throw.

Frequently Asked Questions

When values may exceed the 32-bit int range.
Yes for typical ToInt64 overloads.
Yes—2, 8, 10, or 16.
Parse throws on null; Convert returns 0.
FormatException and OverflowException.

Next: ToSByte()

Convert a value to sbyte (−128…127) with System.Convert.

ToSByte() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful