Solved

convert c++ code to c#

Posted on 2006-06-30
5
322 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
Comment Utility
ok jkr
 plz move it to c# area
0
 
LVL 2

Expert Comment

by:MyersA
Comment Utility
Hi,

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

Thanks.
0
 
LVL 9

Accepted Solution

by:
DrAske earned 500 total points
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now