Solved

convert c++ code to c#

Posted on 2006-06-30
5
324 Views
Last Modified: 2010-04-16
i want to convert this c++ code to c#

////////////////////////////////////////
// conversion implemented here
#include <stdafx.h>
using namespace std;

std::string conv(std::string thestlstring, int base, int base2)
{
      // variable declarations
      int myval;
      int remainder;
      int loopvar = 0;
      int mynumber = 0;
      int testsum = 0;      
      float value1;
      float value2;
      char mychar;
      char element;
      std::string stlstring;
      std::stringstream thestream;
// generate base 10 integer from number in base 'mybase'
      string::iterator pos;
      pos = thestlstring.end();
      
                        for (--pos;pos>=thestlstring.begin();--pos)
                        {
                              mychar = *pos;

                              switch (mychar)
                              {
                              case '0' :
                                    myval = 0;
                                    break;
                              case '1' :
                                    myval = 1;
                                    break;
                              case '2' :
                                    myval = 2;
                                    break;
                              case '3' :
                                    myval = 3;
                                    break;
                              case '4' :
                                    myval = 4;
                                    break;
                              case '5' :
                                    myval = 5;
                                    break;
                              case '6' :
                                    myval = 6;
                                    break;
                              case '7' :
                                    myval = 7;
                                    break;
                              case '8' :
                                    myval = 8;
                                    break;
                              case '9' :
                                    myval = 9;
                                    break;
                              case 'A' :
                                    myval = 10;
                                    break;
                              case 'B' :
                                    myval = 11;
                                    break;
                              case 'C' :
                                    myval = 12;
                                    break;
                              case 'D' :
                                    myval = 13;
                                    break;
                              case 'E' :
                                    myval = 14;
                                    break;
                              case 'F' :
                                    myval = 15;
                                    break;
                              }
                              mynumber += myval * (int)pow(base, loopvar);

                              ++loopvar;
                        }
// generate 'n' - (n-1) = # of digits in base 10 number
// generated above
            for  (int n = 0;testsum < mynumber;++n)
            {
                  testsum = testsum + (int)pow(base2,n) * (base2-1);
            }
// declare array for insertion of digits from the
// converted number
            char* revarray = new char[n-1];

// generate the new number
            for (int i=0;i<n;++i)
            {
                  value1 = (float)((float)mynumber / (float)base2);
                  value2 = (float)floor((float)mynumber / (float)base2);
                  remainder = mynumber - (int)value2*base2;
                  
                  if (remainder < 10)
                  element = char(remainder + '0');
                  else if (remainder == 10)
                        element = 'A';
                  else if (remainder == 11)
                        element = 'B';
                  else if (remainder == 12)
                        element = 'C';
                  else if (remainder == 13)
                        element = 'D';
                  else if (remainder == 14)
                        element = 'E';
                  else if (remainder == 15)
                        element = 'F';
// insert an element each time within the loop
                  revarray[i] = element;
// move down one base multiple
                  mynumber = (mynumber - remainder)/base2;

            }
// create std::string format of converted number
            for(int i2 = n-1; i2>=0; --i2)
            thestream << revarray[i2];

            thestream >> stlstring;
// return the number
            return stlstring;
}
0
Comment
Question by:flashaoy
5 Comments
 
LVL 1

Author Comment

by:flashaoy
ID: 17020737
ok jkr
 plz move it to c# area
0
 
LVL 2

Expert Comment

by:MyersA
ID: 17021026
Hi,

Out of curiosity, which is the C# TA area?

Thanks.
0
 
LVL 9

Accepted Solution

by:
DrAske earned 500 total points
ID: 17023977
Hi falshaoy..
I think the procedure convert *thestlstring* from base to base2 .. IF SO ??
then this is equivalent in C# ..

public string conv(string str, int base1, int base2)
{
      long base10 = Convert.ToInt32(str,base1);       // convert str to base 10;
      string result = Convert.ToString(base10,base2);// convert base10 to base2;
      return result;
}

regards, Ahmad;
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

809 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