Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7794861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:54:00+00:00 2026-06-01T22:54:00+00:00

the operator int() function converts the string to an int class mystring { private:

  • 0

the operator int() function converts the string to an int

class mystring
{ 
  private:
    chat str[20];

  public:
   operator int()             // i'm assuming this converts a string to an int
   {
     int i=0,l,ss=0,k=1;

     l = strlen(str)-1;
     while(l>=0)
     {
       ss=ss+(str[l]-48)*k;
       l--;
       k*=10;
     }
     return(ss);
   } 
}

int main()
{
  mystring s2("123");
  int i=int(s2);
  cout << endl << "i= "<<i;
}

So what’s the logic behind operator int() ? What’s the 48 in there? Can someone explain to me the algorithm behind the conversion from string to int.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:54:02+00:00Added an answer on June 1, 2026 at 10:54 pm

    Yes this converts a string to an integer. 48 is the ASCII value for ‘0’. If you subtract 48 from an ASCII digit you’ll get the value of the digit (ex: ‘0’ – 48 = 0, ‘1’ – 48 = 1, ..). For each digit, your code calculates the correct power of 10 by using k (ranges between 1…10^{ log of the number represented by the input string}).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: class C { public: void operator =(int i) {} }; class SubC :
Given the following: StreamLogger& operator<<(const char* s) { elements.push_back(String(s)); return *this; } StreamLogger& operator<<(int
I use operator() as a subscript operator this way: double CVector::operator() (int i) const
How can this code compile? The code below in the operator int CAN access
I have this code: #include <iostream> #include <functional> struct A { int operator()(int i)
I'm writing a logging class that uses a templatized operator<< function. I'm specializing the
I've this class namespace baseUtils { template<typename AT> class growVector { int size; AT
I have code like this: CREATE FUNCTION gantistok (@id VARCHAR(8), @id_t INT) RETURNS INT
I have this declaration struct Z { void operator ()( int a ) {
Consider the following setup: I am given an interface template<class T> void FooClass<T>::foo(boost::function<double (int)>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.