Convert Hex String to Hex Number in C#



Firstly, set the Hex String −

string str = "7D";

Now, use the Convert.ToSByte() method to convert the Hex string to Hex number −

Console.WriteLine(Convert.ToSByte(str, 16));

Let us see the complete code −

Example

 Live Demo

using System;

namespace Demo {
   public class Program {
      public static void Main(string[] args) {
         string str = "7D";
         Console.WriteLine(Convert.ToSByte(str, 16));
      }
   }
}

Output

125

Another way of converting Hex String to Hex Number −

Example

 Live Demo

using System;

namespace Demo {
   public class Program {
      public static void Main(string[] args) {
         string str = "7D";
         Console.WriteLine(Convert.ToInt32(str, 16));
      }
   }
}

Output

125
Updated on: 2020-06-22T13:04:03+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements