
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to convert an integer to hexadecimal and vice versa in C#?
Converting Integer to Hexadecimal
An integer can be converted to a hexadecimal by using the string.ToString() extension method.
Integer Value: 500 Hexadecimal Value: 1F4
Converting Hexadecimal to Integer −
A hexadecimal value can be converted to an integer using int.Parse or convert.ToInt32
int.Parse − Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.
Hexadecimal Value: 1F4 Integer Value: 500
Convert.ToInt32 −Converts a specified value to a 32-bit signed integer.
Hexadecimal Value: 1F4 Integer Value: 500
Converting Integer to Hexadecimal −
string hexValue = integerValue.ToString("X");
Example
using System; namespace DemoApplication{ public class Program{ public static void Main(){ int integerValue = 500; Console.WriteLine($"Integer Value: {integerValue}"); string hexValue = integerValue.ToString("X"); Console.WriteLine($"Hexadecimal Value: {hexValue}"); Console.ReadLine(); } } }
Output
The output of the above code is
Integer Value: 500 Hexadecimal Value: 1F4
Converting Hexadecimal to Integer −
Example using int.Parse −
Example
using System; namespace DemoApplication{ public class Program{ public static void Main(){ string hexValue = "1F4"; Console.WriteLine($"Hexadecimal Value: {hexValue}"); int integerValue = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber); Console.WriteLine($"Integer Value: {integerValue}"); Console.ReadLine(); } } }
Output
The output of the above code is
Hexadecimal Value: 1F4 Integer Value: 500
Example using Convert.ToInt32 −
Example
using System; namespace DemoApplication{ public class Program{ public static void Main(){ string hexValue = "1F4"; Console.WriteLine($"Hexadecimal Value: {hexValue}"); int integerValue = Convert.ToInt32(hexValue, 16); Console.WriteLine($"Integer Value: {integerValue}"); Console.ReadLine(); } } }
Output
The output of the above code is
Hexadecimal Value: 1F4 Integer Value: 500
- Related Articles
- How to Convert a String to Hexadecimal and vice versa format in java?
- How to convert an array to Set and vice versa in Java?
- How to convert binary to decimal and vice versa in Perl?
- How to convert String to StringBuilder and vice versa Java?
- Convert from any base to decimal and vice versa in C++
- C++ Program to Convert Binary Number to Octal and vice-versa
- C++ Program to Convert Binary Number to Decimal and vice-versa
- C++ Program to convert Octal Number to Decimal and vice-versa
- Convert string to DateTime and vice-versa in Python
- How to Convert CFAbsoluteTime to Date Object and vice-versa in JavaScript?
- C++ Program to Convert Km/hr to miles/hr and vice versa
- How to convert an integer to a hexadecimal string in Python?
- How to Convert Letter to Number or Vice Versa in Excel?
- Java Program to Convert the LinkedList into an Array and vice versa
- How to Convert Gallon to Liter/Ounce or Vice Versa in Excel?

Advertisements