How to convert hexadecimal "string" into actual hexadecimal value in .NET? [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert numbers between hexadecimal and decimal in C#?
I need to be able to take a hexadecimal string and convert it into actual hexadecimal value in .NET. How do I do this?
For instance, in Delphi, you can take string of "FF" and add the dollar sign as follow to it.
tmpstr := '$'+ 'FF';
Then, convert tmpstr string variable into an integer to get the actual hexidecimal. The result would be 255.

Assuming you are trying to convert your string to an int:
var i = Int32.Parse("FF", System.Globalization.NumberStyles.HexNumber)
Your example 1847504890 does not fit on an int, however. Use a longer type instead.
var i = Int64.Parse("1847504890", System.Globalization.NumberStyles.HexNumber)

Very simple:
int value = Convert.ToInt32("DEADBEEF", 16);

You can do it by following
string tmpstr = "FF";
int num = Int32.Parse(tmpstr, System.Globalization.NumberStyles.HexNumber);
You can also see the link Converting string to hex

int hexval = Convert.ToInt32("FF", 16);

Related

how to use Any(char.IsDigit) to extract the int value [duplicate]

This question already has answers here:
How can I parse the int from a String in C#?
(4 answers)
Closed 3 months ago.
I have this line of code
bool containsInt = "Sanjay 400".Any(char.IsDigit)
What I am trying to do is extract the 400 from the string but
Any(char.IsDigit)
only returns a true value. I am very new to coding and c# especially.
As you already found out, you cannot use Any for extraction.
You would need to use the Where method:
List<char> allInts = "Sanjay 400".Where(char.IsDigit).ToList();
The result will be a list containing all integers/digits from your string.
Ok if you are interested in the value as integer you would need to convert it again to a string and then into an integer. Fortunately string has a nice constructor for this.
char[] allIntCharsArray = "Sanjay 400".Where(char.IsDigit).ToArray();
int theValue = Convert.ToInt32(new string(allIntCharsArray));
If you are using .NET 5 or higher you could also use the new cool TryParse method without extra string conversion:
int.TryParse(allIntCharsArray, out int theValue);
int result = int.Parse(string.Concat("Sanjay 400".Where(char.IsDigit)));
Use the Regex
var containsInt = Convert.ToInt32(Regex.Replace(#"Sanjay 400", #"\D", ""));
Regular expressions allow you to take only numbers and convert them into integers.

Convert hexadecimal string to long in the same format [duplicate]

This question already has answers here:
Convert integer to hexadecimal and back again
(11 answers)
Closed 2 years ago.
I have this code:
string IDNumber = "0x0037D70D";
long x = 0x0037D70D; //Working
long x = long.Parse(IDNumber); //Error Input string was not in a correct format.
Required to send the string above (IDNumber) to long y and should maintain the variable format and value typical as its start with 0x.... same as in the IDNumber string
Kindly help me.
Edit:
I have function in DLL file, this function accept one parameter with data type long
If I give this long parameter the value like 0x0037D70D then the function is working correctly and do the required job but if I give the long parameter the value in any other format like 3659533 function is not working
string example1 = "0x0037D70D";
long example2 = 0x0037D70D;
At the end I have the value coming in string format like example1 which I want to convert to be like example2 because if I have the value written like example2 format and saved in long variable then is working
Update:
The problem solved, I use this function to communicate with external hardware device and after many times trying the device hangs, I rest the device and the solution advised by #Kirill Polishchuk working for me.
long l = Convert.ToInt64(IDNumber, 16);
I would suggest using Convert class:
long l = Convert.ToInt64(IDNumber, 16);
You should remove 0x prefix:
long y = long.Parse(IDNumber.Replace("0x", ""), System.Globalization.NumberStyles.HexNumber);
long x = 0x0037D70D; //Working
Console.WriteLine(x.ToString("X")); //prints "37D70D", no prefix

How to convert value of hex String to double in c#? [duplicate]

This question already has answers here:
Convert Hex to Double
(4 answers)
Closed 5 years ago.
I have a hexadecimal value saved in a string, that I want to convert it to a double.
For example, I have string temp = "08E97091", and after conversion the result should be 14.9516433.
You can use BitConverter class or you can do something like this :
For eg:
byte x = Convert.ToByte(hexValueInString, 16);
double doubleX = Convert.ToDouble(x);
string hexnumber = "00c6";
double doubleValue = (double)Convert.ToInt32(hexnumber, 16);

Hex long number to string that looks the same [duplicate]

This question already has answers here:
Convert a number into the hex value in .NET
(2 answers)
Closed 6 years ago.
I've got a simple question i have got a long value presented in this way
long value = 0x001f0347
Now is there's a way to convert it to string that looks the same:
string value = "0x001f0347";
I have tried some converters but no luck.
Try formatting ("x8" format string - 8 hexadecimal digits):
long value = 0x001f0347;
string result = "0x" + value.ToString("x8");
If you prefer Convert then convert using toBase == 16 and pad left up to 8 symbols
string result = "0x" + Convert.ToString(value, 16).PadLeft(8, '0');

Why does Convert.ToInt32('1') returns 49? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
c# convert char to int
This works:
int val = Convert.ToInt32("1");
But this doesn't:
int val = Convert.ToInt32('1'); // returns 49
Shouldn't this convert to actual int?
It is returning the ASCII value of character 1
The first statement treats the argument as string and converts the value into Int,
The second one treats the argument as char and returns its ascii value
As the others said, Convert returns the ASCII code.
If you want to convert '1' to 1 (int) you should use
int val = Convert.ToInt32('1'.ToString());
The code '1' is the same as (char)49 (because the Unicode code point of the character 1 is 49). And Convert.ToInt32(char) returns the code point of that character as an int.
As others have already pointed out: In your second example ('1') you are using a char literal. A char is a numeric type. There is no parsing done as in the string example ("1"), since it already is a number - just a cast to a wider number format (from 16 bits to 32 bits).
It treats '1' as char and int form of any char is its ASCII equivalent so it return its ASCII equivalent.
But in case of "1" it treats it as string and convert it to integer.

Categories