Solved

convert c++ code to c#

Posted on 2006-06-30
5
325 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Question! 4 39
Hey Audio! 9 45
Hey!!!! 1 29
.NET Enums [Flags] & Bitwise Design Question 6 29
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

828 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