Convert.ToInt32 not accepting string hexadecimal values [duplicate] - c#

This question already has answers here:
Convert integer to hexadecimal and back again
(11 answers)
How to Convert Hex String to Hex Number [duplicate]
(2 answers)
Closed 4 years ago.
I'm currently making a program which saves a bunch of memory addresses to a class automatically. However,
Convert.ToInt32(value)
doesn't seem to want to accept the string value "0xB24C" as a valid integer.
'Input string was not in a correct format.'
Actually I'm able to just save offsets as integers like this public const Int32 m_ArmorValue = 0xB24C;
Here's my code where I assign the integer;
hazedumper.netvars.m_ArmorValue = Convert.ToInt32(value);
value being the string offset "0xB24C"
Can anyone tell me why this error is occurring or is it not possible to convert a string memory address/hexadecimal value to an Int32.

Convert.ToInt32() takes the base as second parameter. Furthermore you must remove the 0x prefix. See the docs for details
var s="0xA123";
var i = Convert.ToInt32(s.Substring(2), 16);

Related

converting string to float gives an error [duplicate]

This question already has answers here:
Best way to parse float?
(11 answers)
Closed 12 months ago.
I want to convert a value from string to float in c#. the value definitely has precision(I mean .00 or .67 in this example. I don't know the word in English for sure) like "120.00" , "233.67"
and this is what I do for doing that
float existingValue = float.Parse(param[1].ToString());
param[1] contains the string value
but gives me error :
'Input string was not in a correct format.'
If I change the value from "120.00" to "120" it works fine
what is the problem for that?
You should use an overload of float.Parse method. Specify IFormatProvider as an argument (e. g. CultureInfo.InvariantCulture or a specifc one) or NumberStyles.

how to convert a formula stored in a string to double? [duplicate]

This question already has answers here:
Evaluating string "3*(4+2)" yield int 18 [duplicate]
(13 answers)
Closed 7 years ago.
I have a string with values stored as:
string formula = "(10.5+10.5)/(2*5)";
double a=convert.ToDouble((10.5+10.5)/(2*5)) is working fine but i need to solve it like double a=convert.ToDouble(formula). Is it possible? I'm geeting error input string is not in correct format.
You cannot simply pass a formula to Convert.ToDouble and expect that it will calculate it. You need to compute the formula.
The good way is to implement a parser, which will build an expression tree, and calcultate the value.
Another hacky solution is to utilize Compute method of DataTable:
System.Data.DataTable table = new System.Data.DataTable();
decimal result = (decimal)table.Compute(formula);
Note that it returns decimal object. You can now use Convert to make it double, if you need:
double resultDouble = Convert.ToDouble(result);

issue in converting hexadecimal to byte array [duplicate]

This question already has answers here:
How do you convert a byte array to a hexadecimal string, and vice versa?
(53 answers)
Closed 8 years ago.
i have string like
string test = "0x527AE53437CAED39D3E2A8B3E90FFC3BA9073B6D933BC05FF6677B5521DC34"
How can i convert the string to byte[]?
use SoapHexBinary in namespace System.Runtime.Remoting.Metadata.W3cXsd2001
string s = "0x527AE53437CAED39D3E2A8B3E90FFC3BA9073B6D933BC05FF6677B5521DC34";
byte[] num = SoapHexBinary.Parse(s.Substring(2)).Value;
That string is hexadecimal.
If you want to convert it to a numeric you're going to need special handling - that's a very big number and will overflow the basic types.
If it was a reasonable size, all numeric types are agnostic of base-representation. To see a hexadecimal version of a number, you simply call .ToString("X") on it.
EDIT
My answer was based on the initial version of the question before the byte[] was specified. There is a previous SO question and answer for this: How can I convert a hex string to a byte array?

format decimal number using ToString [duplicate]

This question already has answers here:
Using String Format to show decimal up to 2 places or simple integer
(18 answers)
Closed 9 years ago.
If I got decimal number like 14.50 and I want to be represented like decimal 10.2
0000000014.50
how can I do this?
Thank you
Use custom numeric format string:
var value = 14.50m;
string valueString = value.ToString("0000000000.00");
0 is a placeholder: Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
If you don't have an issue with the data type being converted to string then you could use Padding in c#.
Refer the link below :
http://msdn.microsoft.com/en-us/library/66f6d830(v=vs.100).aspx

Converting string of binary to hex [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting long string of binary to hex c#
nyI'm looking for a way to convert a string of binary to a hex string.
the binary string has four positions.
the binary string looks something like this
string binarystring= "1011";
output string should be like this
output string="B";
is there any way to convert a string of binary into hex?
Convert.ToInt32("1011", 2).ToString("X");
For more information about the string value used with ToString() as its parameter, check the following documentation:
https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx

Categories