Home > Backend Development > C#.Net Tutorial > How to convert integer to hexadecimal and vice versa in C#?

How to convert integer to hexadecimal and vice versa in C#?

王林
Release: 2023-09-11 09:37:02
forward
955 people have browsed it

在 C# 中如何将整数转换为十六进制,反之亦然?

Convert an integer to hexadecimal

You can use the string.ToString() extension method to convert an integer to hexadecimal.

Integer Value: 500
Hexadecimal Value: 1F4
Copy after login
Copy after login

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
Copy after login
Copy after login
Copy after login
Copy after login

Convert.ToInt32 - Convert Converts the specified value to a 32-bit signed integer.

Hexadecimal Value: 1F4
Integer Value: 500
Copy after login
Copy after login
Copy after login
Copy after login

Converting Integer to Hexadecimal

string hexValue = integerValue.ToString("X");

Example

Live Demo

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();
      }
   }
}
Copy after login

Output

The output of the above code is

Integer Value: 500
Hexadecimal Value: 1F4
Copy after login
Copy after login

Converting Hexadecimal to Integer

Example using int.Parse

Example

Live Demo

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();
      }
   }
}
Copy after login

Output

The output of the above code is

Hexadecimal Value: 1F4
Integer Value: 500
Copy after login
Copy after login
Copy after login
Copy after login

Example of using Convert.ToInt32

Example

Online demonstration

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();
      }
   }
}
Copy after login

Output

The output of the above code is

Hexadecimal Value: 1F4
Integer Value: 500
Copy after login
Copy after login
Copy after login
Copy after login

The above is the detailed content of How to convert integer to hexadecimal and vice versa in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template