3

i have a text box on my form. I want to write "0x31" as a string to my textbox and then when i clicked a button, i want to convert this string to 0x31 as a hexadecimal value.

How can i convert this string to hexadecimal value?

Abel
  • 52,738
  • 19
  • 137
  • 227
sanchop22
  • 2,509
  • 10
  • 40
  • 59

6 Answers6

10
int i = Convert.ToInt32("0x31", 16);
Console.WriteLine("0x" + i.ToString("X2"))
L.B
  • 106,644
  • 18
  • 163
  • 208
5
string hexValues = "48 65 6C 6C 6F 20 57 6F 72 6C 64 21";
string[] hexValuesSplit = hexValues.Split(' ');
foreach (String hex in hexValuesSplit)
{
    // Convert the number expressed in base-16 to an integer.
    int value = Convert.ToInt32(hex, 16);
    // Get the character corresponding to the integral value.
    string stringValue = Char.ConvertFromUtf32(value);
    char charValue = (char)value;
    Console.WriteLine("hexadecimal value = {0}, int value = {1}, char value = {2} or {3}",
                    hex, value, stringValue, charValue);
}

Example From: http://msdn.microsoft.com/en-us/library/bb311038.aspx

KingCronus
  • 4,440
  • 1
  • 21
  • 46
2

Hexadecimal is just a representation of an value, it is not a value itself.

This page will tell you everything you need to know about parsing and displaying hex in C#

http://msdn.microsoft.com/en-us/library/bb311038.aspx

KingCronus
  • 4,440
  • 1
  • 21
  • 46
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/20633441) – Jesse Aug 20 '18 at 08:45
  • 1
    @JessedeBruijne This is FAR from a link only answer, and should not be deleted via the LQP queue. – Goodbye StackExchange Aug 20 '18 at 12:31
  • @FrankerZ Take away the link, and what is left? Merely a comment, not an answer. OP asked on how to convert the value, which this comment does not answer. If the post contained a sample of the code provided in the link, it would be valid, but as of now, it's really a link-only answer, and even if it's not, it's certainly not "FAR from a link only answer". – Jesse Aug 20 '18 at 12:49
  • Ironically, the accepted answer has the exact same link in it, but is still a valid answer if the linked page would change. – Jesse Aug 20 '18 at 12:50
1

The string hex value is a representation of a value. The actual string value can be converted to whatever you like(float, int etc.) There are several ways to do the conversion. Simple example:

// convert to int from base 16
int value = Convert.ToInt32(hex, 16);
TheBoyan
  • 6,380
  • 3
  • 40
  • 56
  • You can have two options: - If your Hex contains the **0x** preffix: ```csharp Convert.ToInt32("Your Hex", 16); ``` - If your HEX doesn't contain the **0x** preffix: ```csharp int.Parse("Your Hex", System.Globalization.NumberStyles.HexNumber); ``` – Sales Lopes Jul 30 '19 at 13:47
1

First to clear up: The string is in hexadecimal format, when you convert it to a value it's just a numeric value, it's not hexadecimal.

Use the Int32.Parse method with the NumberStyle.HexNumber specifier:

string input = "0x31";

int n;
if (input.StartsWith("0x")) {
  n = Int32.Parse(input.Substring(2), NumberStyles.HexNumber);
} else {
  n = Int32.Parse(input);
}
Guffa
  • 640,220
  • 96
  • 678
  • 956
1

Note that hex is just a representation of a value - so what you are really asking is how you can parse a value from the string - do it like so:

int val = int.Parse("0x31", NumberStyles.HexNumber);

val now contains an int with the hex value 0x31.

dice
  • 2,782
  • 1
  • 21
  • 33