-1

My program creates couple string hex numbers and saves it to a .ini file on my computer. Now I want to convert them to int32. In my .ini file the hex values are listed this way:

  • 02E8ECB4
  • 02E8ECB5
  • 02E8ECE7
  • 02E8EC98

and now I want these values in the same .ini file replaced with the new converted values, like:

  • 48819380
  • 48819381
  • 48819431
  • 48819352

That is how i save the values:

            using (StreamWriter writer = new StreamWriter(@"C:\values.ini", true))
            {
                foreach (string key in values.Keys)
                {
                    if (values[key] != string.Empty)
                        writer.WriteLine("{1}", key, values[key]);
                }

            }
  • 2
    What is values? How is defined and loaded? Your WriteLine is wrong, There are two arguments but only a placeholder {1} only for the last argument. But your _question_ lacks the most important thing. What is your question? – Steve Jul 09 '16 at 11:25

2 Answers2

0

If your concern is how to parse each of these strings and create the corresponding int for each of them you could try the following:

int val;
if(int.TryParse(str, System.Globalization.NumberStyles.HexNumber, out val))
{
    // The parsing succeeded, so you can continue with the integer you have 
    // now in val. 
}

where str is the string, for instance the "02E8ECB4".

Christos
  • 53,228
  • 8
  • 76
  • 108
0

Use ' int.Parse() to convert the values from hex to decimal like this:

using (StreamWriter writer = new StreamWriter(@"C:\values.ini", true))
{
    foreach (string key in values.Keys)
    {
        if (values[key] != string.Empty)
        {
            int hex;
            if(int.TryParse(values[key], System.Globalization.NumberStyles.HexNumber, out hex))
            {
                writer.WriteLine("{1}", key, hex);
            }
            else
            {
                //Replace this line with your error handling code.
                writer.WriteLine("Failed to convert {1}", key, values[key]);
            }
        }
    }
}
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55