Represent Int64 as a Hexadecimal String in C#

To represent Int64 as a hexadecimal string in C#, use the Convert.ToString() method and set the base as 16 for hexadecimal conversion. Int64 represents a 64-bit signed integer that can hold values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Syntax

Following is the syntax for converting Int64 to hexadecimal string −

Convert.ToString(longValue, 16)

You can also use the ToString() method with format specifiers −

longValue.ToString("X")   // Uppercase hex
longValue.ToString("x")   // Lowercase hex

Using Convert.ToString() Method

The Convert.ToString() method with base 16 converts an Int64 value to its hexadecimal representation −

using System;

class Demo {
    static void Main() {
        long val = 947645;
        Console.WriteLine("Long: " + val);
        Console.WriteLine("Hex String: " + Convert.ToString(val, 16));
        
        // Testing with negative number
        long negVal = -123456;
        Console.WriteLine("Negative Long: " + negVal);
        Console.WriteLine("Hex String: " + Convert.ToString(negVal, 16));
    }
}

The output of the above code is −

Long: 947645
Hex String: e75bd
Negative Long: -123456
Hex String: fffffffffffe1dc0

Using ToString() with Format Specifiers

The ToString() method with format specifiers provides more control over the hexadecimal output format −

using System;

class Demo {
    static void Main() {
        long val = 255;
        
        Console.WriteLine("Value: " + val);
        Console.WriteLine("Uppercase hex: " + val.ToString("X"));
        Console.WriteLine("Lowercase hex: " + val.ToString("x"));
        Console.WriteLine("8-digit hex: " + val.ToString("X8"));
        Console.WriteLine("16-digit hex: " + val.ToString("X16"));
    }
}

The output of the above code is −

Value: 255
Uppercase hex: FF
Lowercase hex: ff
8-digit hex: 000000FF
16-digit hex: 00000000000000FF

Comparison of Methods

Method Output Format Use Case
Convert.ToString(val, 16) Lowercase, minimal digits Basic conversion
val.ToString("X") Uppercase, minimal digits Standard hex format
val.ToString("x") Lowercase, minimal digits Lowercase hex format
val.ToString("X8") Uppercase, fixed width Padded hex output

Working with Large Int64 Values

Here's an example with maximum and minimum Int64 values −

using System;

class Demo {
    static void Main() {
        long maxVal = Int64.MaxValue;
        long minVal = Int64.MinValue;
        
        Console.WriteLine("Max Int64: " + maxVal);
        Console.WriteLine("Max as hex: " + Convert.ToString(maxVal, 16));
        
        Console.WriteLine("Min Int64: " + minVal);
        Console.WriteLine("Min as hex: " + Convert.ToString(minVal, 16));
    }
}

The output of the above code is −

Max Int64: 9223372036854775807
Max as hex: 7fffffffffffffff
Min Int64: -9223372036854775808
Min as hex: 8000000000000000

Conclusion

Converting Int64 to hexadecimal in C# can be accomplished using Convert.ToString(value, 16) for basic conversion or ToString("X") for more formatting control. Both methods handle negative numbers by using two's complement representation in the hexadecimal output.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements