C# Alphanumeric string to long conversion [duplicate] - 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);

Related

Format string with variable in HEX format [duplicate]

This question already has answers here:
Format int to hex string
(2 answers)
Closed 1 year ago.
I know I can include variables into string:
int a =1
String s = $"a={a}";
This will show line:
a=1
But how to show answer in HEX format as below?
a=0x01
int a = 1;
string s = $"a=0x{a:x2}";

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);

Convert string to double, [duplicate]

This question already has answers here:
How do I parse a string with a decimal point to a double?
(19 answers)
Closed 5 years ago.
I have a string array which contains 4 elements. Which looks like this.
How ever, when trying to do this:
Vector newVector = new Vector(
(float)Convert.ToDouble(words[1]),
(float)Convert.ToDouble(words[2]));
I get the following error:
'Input string was not in a correct format.'
And that is because it's because the value uses a '.' but if I manually change the array to use a ',' it works.
How can I easiest replace all '.' with ','.
Use
//(float)Convert.ToDouble(words[1]),
(float)Convert.ToDouble(words[1], CultureInfo.InvariantCulture),
Try this...
Vector newVector = new Vector(
(float)Convert.ToDouble(words[1], CultureInfo.GetCultureInfo("en-US").NumberFormat),
(float)Convert.ToDouble(words[2], CultureInfo.GetCultureInfo("en-US").NumberFormat));

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');

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

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);

Categories