?
Solved

convert c++ code to c#

Posted on 2006-06-30
5
Medium Priority
?
329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

762 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