Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 372
• Last Modified:

# 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
0
The_Kingpin08
Asked:
1 Solution

Commented:
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

Commented:
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

Commented:
Um, I meant

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

extractPolygonID(p,&ID);
0

Commented:
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

Data 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

Author 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

Commented:
/*
* 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

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.