VB code to C++ code

One of my friends wrote the following in VB. We would like the code written in C++


Function TextToHTML(ByVal TextToConvert As String) As String
 '
 Dim N, I As Long
 Dim ExtChar, HTML As Variant
 '
 'Special
 'Ext Lowercase
 'Ext Uppercase
 HTML = Array("<", ">", """", "­", "©", "®", "™", "—", "–", "¹", "²", "³", "¶", "·", "«", "»", "¼", "½", "¾", "£", _
              "á", "â", "æ", "à", "å", "ã", "ä", "ç", "é", "ê", "è", "ð", "ë", "í", "î", "ì", "ï", "ñ", "ó", "ô", "ò", "ø", "õ", "ö", "ß", "þ", "ú", "û", "ù", "ü", "ý", "ÿ", _
              "Á", "Â", "Æ", "À", "Å", "Ã", "Ä", "Ç", "Ð", "É", "Ê", "È", "Ë", "Ü", "Ý")
 '
 ExtChar = Array("lt", "gt", "quot", "shy", "copy", "#174", "#153", "#151", "#150", "#185", "#178", "#179", "#182", "#183", "#171", "#187", "#188", "#189", "#190", "#163", _
                 "aacute", "acirc", "aelig", "agrave", "aring", "atilde", "auml", "ccedil", "eacute", "ecirc", "egrave", "eth", "euml", "iacute", "icirc", "igrave", "iuml", "ntilde", "oacute", "ocirc", "ograve", "oslash", "otilde", "ouml", "szlig", "thorn", "uacute", "ucirc", "ugrave", "uuml", "yacute", "yuml", _
                 "Aacute", "Acirc", "AElig", "Agrave", "Aring", "Atilde", "Auml", "Igrave", "Iuml", "Ntilde", "Oacute", "Ocirc", "Ograve", "Oslash", "Otilde", "Ouml", "THORN", "Uacute", "Ucirc", "Ugrave", "Uuml", "Yacute")
 '
 N = 1
 I = LBound(HTML)
 Do
  Do
   N = InStr(N, TextToConvert, HTML(I), vbBinaryCompare)
   If N > 0 Then Exit Do
   I = I + 1
   If I > UBound(HTML) Then Exit Do
   N = 1
  Loop
  If N = 0 Then Exit Do
  TextToConvert = Left$(TextToConvert, N - 1) _
                & "&" & ExtChar(I) & ";" _
                & Right(TextToConvert, Len(TextToConvert) - N)
 Loop
 '
 TextToHTML = TextToConvert
 '
End Function
froozeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
Isn't this the code I rewrote for you?  What was wrong with it?  You accepted somone else's answer so I can't tell what was wrong with it.
0
jflCommented:
Here is our reasons:
 The VB code was too slow.
 Our question had disapeared from the C++ area.

We know that you have answered the other question, but we couldn't make it work. BTW we have more than doubled the points. So we hope you'll rewrite the above VB code in C++.
0
nietodCommented:
Why didn't you tell me what was wrong though?  I would have helped you fix it and it wouldn't cost you more points.  Tha's why I kept asking you if it was working (since I couldn't test it).

Also what do you mean it dissapeard from the C++ area?  If you are suggesting it got lost (I don't think it did, but I have seen this sort of thing happen) you should contact customer service at experts exchange.

Now that I look over the code here it seems that it is similar, but diffferent, than the code I worked on last time.  Is that true (I didn't keep a copy)?  

If you post a brief description of what it is supposed to do, that would help.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

jflCommented:
The VB code above is brand new. Actually made by mrmick.

The function receive a string and returns the string with characters such as Æ, Ø, Å translated to &AElig;, &Oslash; and &Aring;
0
nietodCommented:
answer coming shortly.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
froozeAuthor Commented:
take your time.
0
nietodCommented:
try the following.

#include <string> // Standard Windows include file
using namespace std;

char HTMLAry[]    = {'<',
                     '>'};
char *ExtChrAry[] = {"lt",
                     "gt"};

string TextToHTML(const string TxtStr)
{
   string RetStr;
   int    TxtStrLen = TxtStr.length();

   for (int i = 0; i < TxtStrLen; ++i)
   {
      char CurChr = TxtStr[i];
      int  n;

      for (n = 0; n < sizeof(HTMLAry); ++n)
      {
         if (HTMLAry[n] = CurChr)
            break;
      }
      if (n < sizeof(HTMLAry))
         RetStr += CurChr;
      else
         RetStr += ExtChrAry[n];
   }
   return RetStr;
}

if it doesn't work 100% let me know (I check in 2 or 3 times a day).  Note you have to fill in the two arrays at the top, I didn't bother with those details.

If efficiency is a REAL concern, this code could be optimized some, but it should run pretty fast as is.  Try it an let me know if you need more out of it.
0
froozeAuthor Commented:
Would the code work for both a NT and Unix server? This is BTW a must.

How should I call the function ?
0
nietodCommented:
This is standard, portable C++ code.  It does not contain any code that is platform/hardware dependent.  However, it does use the STL (standard template library) to implement the string class.  The STL is not 100% standard because the standard has not been finalized and been developing over the last 10 years or so.

However the code uses just the basic aspects of the string class, so it should work for any version of C++ that contains even an early version of STL.  Every compiler for NT should have a recent version.  Most UNIX compilers, unless very old and not longer being supported, should also be fine.  

If, for some reason your unix compiler does not have a STL.  You can get the source from a compiler that does (the NT compiler, for example).  The STL source should work with any C++ compiler that supports templates.
0
nietodCommented:
I see the comment on the first line

#include <string> // Standard Windows include file

Is that why you asked about the platform stuff?

Sorry, that comment was a mistake.  I copied the line from one of my programs and changed the include file name.  I didn't change or remove the comment as I should have.
0
froozeAuthor Commented:
> #include <string> // Standard Windows include file
Yes, That frightened me.

Can I just call it like this:
printf (TextToHTML("dis is a test Æ Ø"))

I'm testing. I'll get back to you with my result.
0
froozeAuthor Commented:
I get these errors:

E:\Jfl\HomePage\D-m-f\cgi-src\WebDB\WebDB\Webdb.cpp(43) : error C2230: 'initializing' : indirection to different types
E:\Jfl\HomePage\D-m-f\cgi-src\WebDB\WebDB\Webdb.cpp(43) : error C2015: too many characters in constant
E:\Jfl\HomePage\D-m-f\cgi-src\WebDB\WebDB\Webdb.cpp(43) : error C2230: 'initializing' : indirection to different types
E:\Jfl\HomePage\D-m-f\cgi-src\WebDB\WebDB\Webdb.cpp(43) : error C2015: too many characters in constant
E:\Jfl\HomePage\D-m-f\cgi-src\WebDB\WebDB\Webdb.cpp(636) : error C2501: 'string' : missing decl-specifiers
E:\Jfl\HomePage\D-m-f\cgi-src\WebDB\WebDB\Webdb.cpp(636) : error C2239: unexpected token 'identifier' following declaration of 'string'
E:\Jfl\HomePage\D-m-f\cgi-src\WebDB\WebDB\Webdb.cpp(636) : error C2061: syntax error : identifier 'TextToHTML'
E:\Jfl\HomePage\D-m-f\cgi-src\WebDB\WebDB\Webdb.cpp(639) : error C2065: 'TxtStr' : undeclared identifier
E:\Jfl\HomePage\D-m-f\cgi-src\WebDB\WebDB\Webdb.cpp(639) : error C2228: left of '.length' must have class/struct/union type
E:\Jfl\HomePage\D-m-f\cgi-src\WebDB\WebDB\Webdb.cpp(641) : error C2143: syntax error : missing ';' before 'for'
E:\Jfl\HomePage\D-m-f\cgi-src\WebDB\WebDB\Webdb.cpp(641) : error C2501: 'i' : missing decl-specifiers
E:\Jfl\HomePage\D-m-f\cgi-src\WebDB\WebDB\Webdb.cpp(641) : error C2239: unexpected token '<' following declaration of 'i'
E:\Jfl\HomePage\D-m-f\cgi-src\WebDB\WebDB\Webdb.cpp(641) : error webdb.exe - 102 error(s), 0 warning(s)

0
nietodCommented:
Mine compiled okay, but wasn't tested.  Did you change the code to fill in the arrays?  That could cause the compiler errors (if you made a mistake.)  Judging from the error messages, like "too many characters in constant" that sounds like that could be it.

If you changed the code, post what you've done.  Otherwise...

...Otherwise, post you code anyways as I only gave you a code snippet and the problen could be elsewhere.


0
froozeAuthor Commented:
The following I put in the top of the source code file:

 #include <string>
 using namespace std;

 char HTMLAry[] = {'<', '>', '"', '­', '©', '®', '™', '—', '–', '¹', '²', '³', '¶', '·', '«', '»', '¼', '½', '¾', '£', 'á', 'â', 'æ', 'à', 'å', 'ã', 'ä', 'ç', 'é', 'ê', 'è', 'ð', 'ë', 'í', 'î', 'ì', 'ï', 'ñ', 'ó', 'ô', 'ò', 'ø', 'õ', 'ö', 'ß', 'þ', 'ú', 'û', 'ù', 'ü', 'ý', 'ÿ', 'Á', 'Â', 'Æ', 'À', 'Å', 'Ã', 'Ä', 'Ç', 'Ð', 'É', 'Ê', 'È', 'Ë', 'Ü', 'Ý'};
 char *ExtChrAry[] = {'lt', 'gt', 'quot', 'shy', 'copy', '#174', '#153', '#151', '#150', '#185', '#178', '#179', '#182', '#183', '#171', '#187', '#188', '#189', '#190', '#163', 'aacute', 'acirc', 'aelig', 'agrave', 'aring', 'atilde', 'auml', 'ccedil', 'eacute', 'ecirc', 'egrave', 'eth', 'euml', 'iacute', 'icirc', 'igrave', 'iuml', 'ntilde', 'oacute', 'ocirc', 'ograve', 'oslash', 'otilde', 'ouml', 'szlig', 'thorn', 'uacute', 'ucirc', 'ugrave', 'uuml', 'yacute', 'yuml', 'Aacute', 'Acirc', 'AElig', 'Agrave', 'Aring', 'Atilde', 'Auml', 'Igrave', 'Iuml', 'Ntilde', 'Oacute', 'Ocirc', 'Ograve', 'Oslash', 'Otilde', 'Ouml', 'THORN', 'Uacute', 'Ucirc', 'Ugrave', 'Uuml', 'Yacute'};


And this somewhere else:


string TextToHTML(const string TxtStr)
 {
  string RetStr;
  int TxtStrLen = TxtStr.length();

  for (int i = 0; i < TxtStrLen; ++i)
   {
    char CurChr = TxtStr[i];
    int n;

    for (n = 0; n < sizeof(HTMLAry); ++n)
     {
      if (HTMLAry[n] = CurChr)
      break;
     }
    if (n < sizeof(HTMLAry))
      RetStr += CurChr;
     else
      RetStr += ExtChrAry[n];
   }
  return RetStr;
 }

0
nietodCommented:
One problem is that C (and C++) use single and double quotes differently.  Single quotes define a character i.e a byte.  Double quotes define a string.  To be more specific, an array of characters that may be any length and is terminated by a NUL (0) character (automatically add by the compiler).

The HTMLAry is an array of characters and should use single quotes (or integer values in the byte range).

The ExtChrAry is an array of pointers to strings and should use double quotes.  

By the way, the two arrays should have been declared as constant.  It shouldn't matter if the code is okay, but declaring them constant can help the compiler detect mistakes in the code that attempt to change the arrays.  Try

const char HTMLAry[] = {....
const char const *ExtChrAry[] = {...
0
jflCommented:
Almost there:
using namespace std;

I got this error message:
Webdb.cpp(40) : error C2871: 'std' : does not exist or is not a namespace

0
nietodCommented:
Is that the only error message?  If so, just drop the line.  Namespaces are part of the continualy evolving C++ standard.  The version of STL that I have requires them, yours might not. (You could be in a situation where you need the line for some compilers and not others.  You can use an #if to conditionally include it.)

If there are other errors, ignore this one for the time being.  If there are other errors, make sure that the STL include file "string" is being found.  Otherwise let me know what the other errors are.
0
froozeAuthor Commented:
I then get this error:
error C2166: l-value specifies const object

/////////////////////////////////////////////////////////////////////////////////////////

 // TextToHtml

 #include <string>
// using namespace std;

 const char HTMLAry[] = {'<', '>', '"', '­', '©', '®', '™', '—', '–', '¹', '²', '³', '¶', '·', '«', '»', '¼', '½', '¾', '£', 'á', 'â', 'æ', 'à', 'å', 'ã', 'ä', 'ç', 'é', 'ê', 'è', 'ð', 'ë', 'í', 'î', 'ì', 'ï', 'ñ', 'ó', 'ô', 'ò', 'ø', 'õ', 'ö', 'ß', 'þ', 'ú', 'û', 'ù', 'ü', 'ý', 'ÿ', 'Á', 'Â', 'Æ', 'À', 'Å', 'Ã', 'Ä', 'Ç', 'Ð', 'É', 'Ê', 'È', 'Ë', 'Ü', 'Ý'};
 const char *ExtChrAry[] = {"lt", "gt", "quot", "shy", "copy", "#174", "#153", "#151", "#150", "#185", "#178", "#179", "#182", "#183", "#171", "#187", "#188", "#189", "#190", "#163", "aacute", "acirc", "aelig", "agrave", "aring", "atilde", "auml", "ccedil", "eacute", "ecirc", "egrave", "eth", "euml", "iacute", "icirc", "igrave", "iuml", "ntilde", "oacute", "ocirc", "ograve", "oslash", "otilde", "ouml", "szlig", "thorn", "uacute", "ucirc", "ugrave", "uuml", "yacute", "yuml", "Aacute", "Acirc", "AElig", "Agrave", "Aring", "Atilde", "Auml", "Igrave", "Iuml", "Ntilde", "Oacute", "Ocirc", "Ograve", "Oslash", "Otilde", "Ouml", "THORN", "Uacute", "Ucirc", "Ugrave", "Uuml", "Yacute"};

/////////////////////////////////////////////////////////////////////////////////////////


string TextToHTML(const string TxtStr)
 {
  string RetStr;
  int TxtStrLen = TxtStr.length();

  for (int i = 0; i < TxtStrLen; ++i)
   {
    char CurChr = TxtStr[i];
    int n;

    for (n = 0; n < sizeof(HTMLAry); ++n)
     {
      if (HTMLAry[n] = CurChr)
      break;
     }
    if (n < sizeof(HTMLAry))
      RetStr += CurChr;
     else
      RetStr += ExtChrAry[n];
   }
  return RetStr;
 }
0
nietodCommented:
Ouch!!!

I had a bug and when I told you to put in the "const" for extra securiy if found it!  That is the idea behind const and sometimes it actually works.

the problem is the line

if (HTMLAry[n] = CurChr)

the single equals means copy or assignment.  It tells the compiler to copy CurChr to HTMLAry[n] which is not what we want.  The line should have to equals which means test if the two characters are equal.  It should read

if (HTMLAry[n] == CurChr)

That should fix it.
0
nietodCommented:
Opps "two equals" not "to equals", well you get the idea.
0
froozeAuthor Commented:
That helps!
No errors.   Great :)

Now I only need to know how to call it.
I tried this, but that gave me an error:
printf(TextToHTML("dis is a Æ test"));
error C2664: 'printf' : cannot convert parameter 1 from 'class basic_string<char,struct char_traits<char>,class allocator<char>>' to 'const char *' (new behavior; please see help)
0
nietodCommented:
The problem is not how to call it, but how to use the return value (alternately, how to call printf()). The problem is that the procedure returns an STL string type and printf() expects a const char * type.  However, I was under the impression that the string type would automatically convert to a const char *.  (I'm not to familiar with STL's strings, because I use my own string class.)  I'll do a little research tomorrow to see if I can find something.

You could try an explicit cast like printf((const char *)TextToHTML("asdfasfasfasfasf")).  That might work.
0
froozeAuthor Commented:
I would prefer if I could call it with something like:
TextToHTML("asdfasfasfasfasf")
0
nietodCommented:
That part is fine.  The problem is that TextToHTML() returns an object of type string and printf requires a parameter of type "const char *".  Thus you can do.

string NewStr = TextToHTML("sdgsdgsdgsdgsdfg");

The problem is using the value stored in NewStr.  Its not a big problem, I just don't know the answer right now.

0
nietodCommented:
To convert a string to const char * you use the c_str() member procedure.

You can do

string NewStr = TextToHTML("sdgsdgsdgsdgsdfg");

printf(NewStr.c_str());

or (though harder to read)

printf(TextToHTML("sdgsdgsdgsdgsdfg").c_str());
0
froozeAuthor Commented:
OK, fine

but with:
printf(TextToHTML("ælle bælle øø mig fortælle").c_str());

I get:
æOumlOumlOumlOumlOumlæOumlOumlOumlOumløøOumlOumlOumlOumlOumlOumlOumlOumlOumlæOumlOumlOuml
0
nietodCommented:
In you original question you didn't say the code had to work!

...Hold on.
0
nietodCommented:
Found it, the if is backwards, should be

if (n < sizeof(HTMLAry))
   RetStr += ExtChrAry[n];
else
   RetStr += CurChr;

I tested it and it seems to work now.  (At least as far as what I believe it should be doing.)
0
jflCommented:
I think it's doing the right thing now, but how do I make the following come true:
RetStr += "&" ExtChrAry[n] ";";

In front of ExtChrAry[n] there must be a "&" and after there must be a ";"
0
nietodCommented:
RetStr += "&" + ExtChrAry[n] + ";";  

should work, however.

RetStr += "&";
RetStr += ExtChrAry[n];
RetStr += ";";

is probably a little more efficient.  I was wondering if something like that was needed.  Converting the symbols to regular words didn't seem to make sense.
0
froozeAuthor Commented:
Perfect!

I hope your ok with the points.
0
nietodCommented:
Good--finally.  That's a lot of problems for 15 lines!

The points are fine.  Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.