Solved

VB code to C++ code

Posted on 1998-01-06
32
348 Views
Last Modified: 2008-03-17
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
0
Comment
Question by:frooze
  • 18
  • 10
  • 4
32 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1177552
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
 

Expert Comment

by:jfl
ID: 1177553
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
 
LVL 22

Expert Comment

by:nietod
ID: 1177554
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
 

Expert Comment

by:jfl
ID: 1177555
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
 
LVL 22

Accepted Solution

by:
nietod earned 150 total points
ID: 1177556
answer coming shortly.
0
 

Author Comment

by:frooze
ID: 1177557
take your time.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177558
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
 

Author Comment

by:frooze
ID: 1177559
Would the code work for both a NT and Unix server? This is BTW a must.

How should I call the function ?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177560
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
 
LVL 22

Expert Comment

by:nietod
ID: 1177561
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
 

Author Comment

by:frooze
ID: 1177562
> #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
 

Author Comment

by:frooze
ID: 1177563
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
 
LVL 22

Expert Comment

by:nietod
ID: 1177564
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
 

Author Comment

by:frooze
ID: 1177565
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
 
LVL 22

Expert Comment

by:nietod
ID: 1177566
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
 

Expert Comment

by:jfl
ID: 1177567
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 22

Expert Comment

by:nietod
ID: 1177568
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
 

Author Comment

by:frooze
ID: 1177569
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
 
LVL 22

Expert Comment

by:nietod
ID: 1177570
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
 
LVL 22

Expert Comment

by:nietod
ID: 1177571
Opps "two equals" not "to equals", well you get the idea.
0
 

Author Comment

by:frooze
ID: 1177572
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
 
LVL 22

Expert Comment

by:nietod
ID: 1177573
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
 

Author Comment

by:frooze
ID: 1177574
I would prefer if I could call it with something like:
TextToHTML("asdfasfasfasfasf")
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177575
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
 
LVL 22

Expert Comment

by:nietod
ID: 1177576
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
 

Author Comment

by:frooze
ID: 1177577
OK, fine

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

I get:
æOumlOumlOumlOumlOumlæOumlOumlOumlOumløøOumlOumlOumlOumlOumlOumlOumlOumlOumlæOumlOumlOuml
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177578
In you original question you didn't say the code had to work!

...Hold on.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177579
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
 

Expert Comment

by:jfl
ID: 1177580
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
 
LVL 22

Expert Comment

by:nietod
ID: 1177581
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
 

Author Comment

by:frooze
ID: 1177582
Perfect!

I hope your ok with the points.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177583
Good--finally.  That's a lot of problems for 15 lines!

The points are fine.  Thanks!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now