Ignore:
Timestamp:
04/20/11 15:24:14 (14 years ago)
Author:
tbretz
Message:
Changed the GetHex function; added support for hex and octal numbers.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/Converter.cc

    r10387 r10425  
    211211T Converter::Get(std::stringstream &line) const
    212212{
     213    char c;
     214    line >> c;
     215
     216    if (c=='0')
     217    {
     218        if (line.peek()==-1)
     219            return 0;
     220
     221        if (line.peek()=='x')
     222            line >> hex;
     223        else
     224            line >> oct;
     225
     226        line.unget();
     227    }
     228
     229    line.unget();
     230
    213231    T val;
    214232    line >> val;
     
    331349    return stream.str();
    332350}
    333 
    334 // --------------------------------------------------------------------------
    335 //
    336 //! Converts from a binary block into a hex representation.
    337 //!
    338 //! @param dat
    339 //!     Pointer to the data block
    340 //!
    341 //! @param size
    342 //!     Size of the data block
    343 //!
    344 //! @param chunk
    345 //!     Size of the size of hex chunks seperted by a ':' in bytes
    346 //!
    347 //! @returns
    348 //!     The string
    349 //!
    350 string Converter::GetHex(const void *dat, size_t size, size_t chunk)
    351 {
    352     const unsigned char *ptr = reinterpret_cast<const unsigned char *>(dat);
    353 
    354     ostringstream text;
    355 
    356     text << hex;
    357 
    358     for (size_t i=0; i<size; i++)
    359     {
    360         text << setw(2) << setfill('0') << (unsigned int)ptr[i];
    361         if ((i%chunk)==chunk-1)
    362             text << ":";
    363     }
    364 
    365     return text.str();
    366 }
    367 
    368351
    369352// --------------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.