CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2
  • Faltering Windows support
  • Internet Explorer 8 Click Clever Click Safe
  • Release Candidate 2 for ASP.NET MVC 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 11th, 2009, 09:45 PM
    mwoods mwoods is offline
    Member
     
    Join Date: Aug 2009
    Posts: 38
    mwoods is an unknown quantity at this point (<10)
    Conversion from signed string to unsigned

    Any ideas for a more efficient and safer conversion from signed string to unsigned?

    Code:
    std::basic_string<unsigned char> string_to_unsigned(const std::string& s)
    {
        int size = s.size();
        const char* p;
        std::basic_string<unsigned char> output;
        output.reserve(size);
    
        if (size)
        {
            p = &s[0];
            for (int i = 0; i < size; ++i)
            {
                output.push_back(*((const unsigned char*)p));
    
                ++p;
            }
        }
    
        return output;
    }
    Reply With Quote
      #2    
    Old November 12th, 2009, 01:01 AM
    laserlight laserlight is online now
    Elite Member
    Power Poster
     
    Join Date: Jan 2006
    Location: Singapore
    Posts: 4,123
    laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+)
    Re: Conversion from signed string to unsigned

    I do not know if this is faster, but it certainly is simpler:
    Code:
    inline std::basic_string<unsigned char> string_to_unsigned(const std::string& s)
    {
        return std::basic_string<unsigned char>(s.begin(), s.end());
    }
    __________________
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful
    Reply With Quote
      #3    
    Old November 12th, 2009, 09:33 AM
    mwoods mwoods is offline
    Member
     
    Join Date: Aug 2009
    Posts: 38
    mwoods is an unknown quantity at this point (<10)
    Re: Conversion from signed string to unsigned

    Quote:
    Originally Posted by laserlight View Post
    I do not know if this is faster, but it certainly is simpler:
    Code:
    inline std::basic_string<unsigned char> string_to_unsigned(const std::string& s)
    {
        return std::basic_string<unsigned char>(s.begin(), s.end());
    }
    That seems to be more sensible. Although, I didn't contemplate anything similar at first because I wasn't sure whether implicit conversions between class types were allowed in C++.
    Reply With Quote
      #4    
    Old November 12th, 2009, 11:35 AM
    Lindley Lindley is offline
    Elite Member
    Power Poster
     
    Join Date: Oct 2007
    Location: Fairfax, VA
    Posts: 6,898
    Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+)
    Re: Conversion from signed string to unsigned

    The only conversion required by laserlight's code is that a char be implicitly convertible to an unsigned char. Which it is.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 11:00 AM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.