1

So I've created an XML file that will configure an application that has already been built. Up until now, it was hard-coded with constants that represented offset values in flash memory.

enum {
    MAIN_FLASH_OFFSET      = 0x01000000,
    LOADER_FLASH_OFFSET    = 0x01e00000,
    MICROCODE_FLASH_OFFSET = 0x00060400,
    DRIVER_FLASH_OFFSET    = 0x00100000,
    OTHER_FLASH_OFFSET     = 0x00500000
} ModuleOffsets;

I have modified the application to read from the XML file to configure these offsets based dynamically, based on the graphics card that the user chooses.

In one of my header files, I have added the following to replace the previous enum:

    int MAIN_FLASH_OFFSET;
    int LOADER_FLASH_OFFSET;
    int MICROCODE_FLASH_OFFSET;
    int DRIVER_FLASH_OFFSET;
    int OTHER_FLASH_OFFSET;

Here's my problem. I am using TinyXML to parse the document. Below is a portion of the XML, and my code where I want to pull in these values. When it tries to pull it in, it is having trouble because GetText() returns a string, and the values (0x01000000, etc...) are ints (at least I think.

So how exactly do I store these? I really have no clue, but I feel like I'm close.

XML (the actual file has multiple card entries)

<EGCs xmlns="http://tempuri.org/XMLSchema.xsd">
  <card type="EGC1">
    <offsets>
      <flashOffset>0x01000000</flashOffset>
      <loaderFlashOffset>0x01e00000</loaderFlashOffset>
      <microFlashOffset>0x00060400</microFlashOffset>
      <driverFlashOffset>0x00100000</driverFlashOffset>
      <otherFlashOffset>0x00500000</otherFlashOffset>
    </offsets>
  </card>
</EGCs>

Code

    COFPFileGeneratorDlg ofp;
    TiXmlDocument doc("EGC_Cards.xml"); 
    if(doc.LoadFile())
    {
        TiXmlHandle hDoc(&doc);
        TiXmlElement *pRoot, *pParm;
        pRoot = doc.FirstChildElement("EGCs");
        if(pRoot)
        {
            pParm = pRoot->FirstChildElement("card");
            while(pParm)
            {
                if(pParm && pParm->Attribute("type") == m_dConfigDlg.m_strEGCType)
                {
                    ofp.MAIN_FLASH_OFFSET = pRoot
                        ->FirstChildElement("card")
                        ->FirstChildElement("flashOffset")
                        ->GetText();

                        [...]
                        // close braces
Jon
  • 3,154
  • 13
  • 53
  • 96
  • `0x01000000` is a hexadecimal *representation* of the number known as `16777216` in decimal ;-) A *representation* of a number (often a *string*) is not [necessarily] the number, but can be converted to the number. So the question really is "How can I turn a hexadecimal string into a number?" There are many answers to this on SO :) –  Jun 29 '11 at 20:16
  • See [C++ convert hex string to signed integer](http://stackoverflow.com/questions/1070497/c-convert-hex-string-to-signed-integer) for instance. (Leaving as open because tinyxml may have it's own nifty tricks, I know not.) –  Jun 29 '11 at 20:20
  • I don't work much in binary/hex so I didn't recognize it. Sounds like a fairly easy solution. Thanks guys! – Jon Jun 29 '11 at 20:37

1 Answers1

2

Convert the string to a string stream and read the value into your integer. Using atoi(abc) would probably work also.

#include <ios>
#include <sstream>

int i;
string abc = "0x001";

stringstream convert ( abc );

convert>> std::hex >> i;

The majority of that code came from a 10 second google search.

Connman
  • 158
  • 3
  • I just didn't know WHAT to Google search. I'm a pretty crafty Googler, so if I'd recognized the hex, I wouldn't have had a problem! :D Thanks for the help :) – Jon Jun 29 '11 at 20:38