在vc中如何实现象asp中的htmlencode和htmldecode,就是编码解码,请多帮忙。

解决方案 »

  1.   

    用CString或者string,比如CString:void HtmlEncode(CString &sz)
    {
    sz.Replace("<", "&lt");
    sz.Replace(">", "&gt");
    ...
    }效果:
    输入:"The image tag: <img>",输出:"The image tag: &lt;img&gt"
    其他编码方式类似实现,解码也是类似实现
      

  2.   


    #include <algorithm>
    #include <iostream>
    #include <string>#define array_length(array) (sizeof (array) / sizeof (array)[0])namespace Raye {
      using namespace std;  struct HTMLReplace {
        string match;
        string replace;
      } codes[] = {
        {"&", "&amp;"},
        {"<", "&lt;"}, 
        {">", "&gt;"}
      };  string HTMLEncode( const string& s )
      {
        string rs = s;    // Replace each matching token in turn
        for ( size_t i = 0; i < array_length( codes ); i++ ) {
          // Find the first match
          const string& match = codes[i].match;
          const string& repl = codes[i].replace;
          string::size_type start = rs.find_first_of( match );      // Replace all matches
          while ( start != string::npos ) {
            rs.replace( start, match.size(), repl );
            // Be sure to jump forward by the replacement length
            start = rs.find_first_of( match, start + repl.size() );
          }
        }    return rs;
      }
    }int main()
    {
      using namespace std;  cout << Raye::HTMLEncode( "template <class T> void foo( const string& bar );" ) << '\n';  return 0;
    }