• C

How to convert char to string ?

Hi all,
I have a char string containing  a name and a number. How can I convert the character's number from char to int ? I tried using a function but it didnt work... For example, if the string is "Mike 2", how can I convert the character "2" into an int ?

char extractPolygonID(char* buf, int ID)
{
    int i=0;
     
    while (buf[i] != ' ') {buf[i] = buf[i++];}
     
     if(!isalpha(buf[i++]))
     {
          sscanf(buf[i++], "%x", &ID);
     }

    return i != EOF;
}

Thanks, Frank
The_Kingpin08Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You have too much '++' in that code, and 'ID' cannot be an int by value.

char extractPolygonID(char* buf, int* ID)
{
   int i=0;
   
     if(!isalpha(buf[i]))
    {
         *ID = (int) (buf[i] - '0'); break;
    }

   return i != EOF;
}
0
jkrCommented:
Oops, now *I* am lacking one :o)

char extractPolygonID(char* buf, int* ID)
{
  int i=0;
 
     if(!isalpha(buf[i]))
   {
        *ID = (int) (buf[i] - '0'); break;

   }
    else i++;

  return i != EOF;
}

And, you'd call that like

char* p =  "Mike 2";
int ID

extractPolygonID(p,&ID);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
Um, I meant

char* p =  "Mike 2";
int ID;

extractPolygonID(p,&ID);
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

grg99Commented:
How about:

char extractPolygonID(char* buf, int * ID)
{
    int i=0;
     
    while ( buf[i] != ' ' && buf[i] != '\0' )  i++;   // skip non-blanks
    while ( buf[i] == ' '  )  i++;                        // skip blanks
     
     if( isdigit( buf[i] )  )
         { sscanf( &buf[i], "%d", ID ); return( 1 ); }
     else
        return( 0 );
}

call it with:    ok = extractPolygonID( Line, &IdNumber );



Thanks, Frank
0
Kent OlsenData Warehouse Architect / DBACommented:
Hi The_Kingpin08,

A char IS an int.  It's an 8-bit integer.  But I think that that is not your real question.  :)

It's my guess that you really want to convert the string "123" to the integer value 123.

If so, it's really easy.  C has several ways to do this.

The atoi() function will convert a string to an integer:

  IntValue = atoi (StringValue);


The atol() function will convert a string to a long integer:

  LongIntValue = atol (StringValue);

You can use the scanf() family of functions:

  sscanf (StringValue, "%d", &IntValue);


You can convert it yourself:

int Result;
char *Ptr;
char *String = "123";

  Result = 0;
  Ptr = String;
  while (*Ptr)
  {
    Result = Result * 10 + (*Ptr - '0');
    Ptr++;
  }


Offhand, I'd suggest atoi().

Good Luck!
Kent
0
The_Kingpin08Author Commented:
grg99, why use the "return(1)" line in your function ? This way the function doesn't return the actual ID number but 1 ?
0
kledbetterCommented:
/*
 * If the format of the data is always "name <space> number", then it's easy to do:
 */
  char *cp = strchr (thestring, ' ');       // look for the space
  int theNum = 0;
  if ( cp != NULL)
     theNum = atoi (cp + 1);

/*
 *  If the format isn't always static, it gets a little trickier.  You'll need to simply look for
  *  the first numeric digit in the string, and then atoi() from that, as in:
 */

   char *cp = thestring;           // point at the string
   int  theNum = 0;

   while ((*cp) && (! isdigit(*cp)))   // find the first digit
       ++cp;
   if (*cp)                                          // if a digit was found
       theNum = atoi (cp);                  //   grab its value

-Keith

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.