Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

convert c++ code to c#

Posted on 2006-06-30
5
Medium Priority
?
332 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

604 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