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
Related
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.
This question already has answers here:
String Interpolation vs String.Format
(7 answers)
Closed 5 years ago.
I'm a beginner to C#. So far I came across several ways that I can use to embed variables in a string value. One of the is String Interpolation which was introduced in C# 6.0. Following code is an example for String Interpolation.
int number = 5;
string myString = $"The number is {number}";
What I want to know is whether there is a benefit of using String Interpolation over the following ways to format a string.
// first way
int number = 5;
string myString = "The number is " + number;
//second way
int number = 5;
string myString = string.Format("The number is {0}", number);
The first way that you have shown will create multiple strings in memory. From memory I think it creates the number.ToString() string, the literal "The number is " string and then the string with name myString
For the second way that you show it's very simple: String interpolation compiles to the string.Format() method call that you use.
EDIT: The second way and the interpolation will also support format specifiers.
A more detailed discussion by Jon Skeet can be found here: http://freecontent.manning.com/interpolated-string-literals-in-c/
This question already has answers here:
How to convert numbers between hexadecimal and decimal
(20 answers)
Closed 7 years ago.
My string is in "abcd123ef" format. I want it to convert in long format like in variable b.
I tried many ways but got exception as 'input string is not in correct format'
Ways I tried are shown below:
1)
var a = "ABCD123Ef";
long b = convert.int64(a);
2)
var a = 'ABCD123Ef';
long b = parse.int64(a);
3)
long b = convert.int64("ABCD123Ef");
If that is a hex value you can convert like this
var b = Convert.ToInt64 (a, 16);
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);
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Convert.ToInt32() a string with Commas
i have a value in the label as: 12,000
and i wish to convert it into an integer like 12000 (use it for comparison)
i tried int k = convert.toint32("12,000"); this does not work.
Thanks
You're being screwed up by the comma. If all of your values have commas in them, you'll want to run a string.replace() to remove them. Once that comma is gone, it should work fine.
A more thorough way would be to Parse it, allowing for thousands.
Try the following
var number = Int32.Parse("12,000", System.Globalization.NumberStyles.AllowThousands);
Try this
string num = "12,000";
int k = Convert.ToInt32(num.Replace(",",""));
string k = "12,000";
int i = Convert.ToInt32(k.Replace(",", ""));
will work