i am making application in c#. In that implication i have string which contain decimal value as
string number="12000";
The Hex equivalent of 12000 is 0x2EE0.
Here i want to assign that hex value to integer variable as
int temp=0x2EE0.
Please help me to convert that number. Thanks in advance.
let stringVal = Convert.ToString decimalVal printfn $"The decimal as a string is {stringVal}."
To convert Python String to hex, use the inbuilt hex() method. The hex() is a built-in method that converts the integer to a corresponding hexadecimal string. For example, use the int(x, base) function with 16 to convert a string to an integer.
The format for coding a hexadecimal string mask is: X'yy...yy' The value yy represents any pair of hexadecimal digits that constitute a byte (8 bits). Each bit must be 1 (test bit) or 0 (ignore bit). You can specify up to 256 pairs of hexadecimal digits.
string input = "Hello World!";
char[] values = input.ToCharArray();
foreach (char letter in values)
{
// Get the integral value of the character.
int value = Convert.ToInt32(letter);
// Convert the decimal value to a hexadecimal value in string form.
string hexOutput = String.Format("{0:X}", value);
Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput);
}
/* Output:
Hexadecimal value of H is 48
Hexadecimal value of e is 65
Hexadecimal value of l is 6C
Hexadecimal value of l is 6C
Hexadecimal value of o is 6F
Hexadecimal value of is 20
Hexadecimal value of W is 57
Hexadecimal value of o is 6F
Hexadecimal value of r is 72
Hexadecimal value of l is 6C
Hexadecimal value of d is 64
Hexadecimal value of ! is 21
*/
SOURCE: http://msdn.microsoft.com/en-us/library/bb311038.aspx
An int contains a number, not a representation of the number. 12000 is equivalent to 0x2ee0:
int a = 12000;
int b = 0x2ee0;
a == b
You can convert from the string "12000" to an int using int.Parse(). You can format an int as hex with int.ToString("X").
Well you can use class String.Format to Convert a Number to Hex
int value = Convert.ToInt32(number);
string hexOutput = String.Format("{0:X}", value);
If you want to Convert a String Keyword to Hex you can do it
string input = "Hello World!";
char[] values = input.ToCharArray();
foreach (char letter in values)
{
// Get the integral value of the character.
int value = Convert.ToInt32(letter);
// Convert the decimal value to a hexadecimal value in string form.
string hexOutput = String.Format("{0:X}", value);
Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With