Search code examples
c#stringhexstreamwriterint32

Convert string lines from a .ini file to int32


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

            }

Solution

  • 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".