Hi,

I have a question which i have no idea how to do manage.

I need to convert a string in the format like "1 234 567" so i can do some calculations on the indvidual numbers.

What is the best way to do this?

Thanks

Hi,

I have a question which i have no idea how to do manage.

I need to convert a string in the format like "1 234 567" so i can do some calculations on the indvidual numbers.

What is the best way to do this?

Hi,

I have a question which i have no idea how to do manage.

I need to convert a string in the format like "1 234 567" so i can do some calculations on the indvidual numbers.

What is the best way to do this?

Could he use something like

``static_cast<int>(string) - 48``

## All 11 Replies

Hi,

I have a question which i have no idea how to do manage.

I need to convert a string in the format like "1 234 567" so i can do some calculations on the indvidual numbers.

What is the best way to do this?

Thanks

Don't know is it the best way but you can do like this. Parse the string until the space (' ') and the number until the space convert to int element of array.

andor: Don't know is it the best way but you can do like this. Parse the string until the space (' ') and the number until the space convert to int element of array.

Thats what i thought i would be able to do but i couldn't get something like that to work. I am probably doing something wrong.

I thought something like below would do the trick but i keep getting cast errors.

`````` while (*string != '\0')
{
for (i = 0; i < strlen(string); i++)
{
converted[i] = atoi((int)string[i]);
}
}
``````

Correct me if I'm wrong...but doesn't atoi() require a C-String as the argument?

``````char input;

code...code...code...

atoi(input);``````

Correct me if I'm wrong...but doesn't atoi() require a C-String as the argument?

it does. i wanted to see if it is possible to send a single part of the array string. ie string then increment to string and so on.

Hi,

I have a question which i have no idea how to do manage.

I need to convert a string in the format like "1 234 567" so i can do some calculations on the indvidual numbers.

What is the best way to do this?

Thanks

Are you using C++ or C? if you're using C++ then investigate stringstreams, which can be found in the <sstream> library - stringstreams make this sort of thing really, really easy!

just c

Could he use something like

``static_cast<int>(string) - 48``

Could he use something like

``static_cast<int>(string) - 48``

I think you should avoid casts as much as possible unless there's no option left or it turns out to be less evil then the other option.

In order of preference
strtol() - does conversion and will report numeric overflow / underflow and tell you how much data was processed
sscanf() - like strtol() without the over/under flow
atoi() - pretty useless atoi("0") and atoi("hello") may both return 0

Maybe something like this is what you are looking for

``````int main (void)
{
int i, j ;
char num_string[] = "1 234 23 45" ;
char tmp  = {'\0', '\0'} ;
int length = strlen (num_string) ;
int* values = (int*) malloc (sizeof (int) * length) ;

for ( i = 0, j = 0; i < length; ++i)
{
if ( isdigit (num_string[i] ) )
{
tmp  = num_string [i] ;
values [j++] = atoi (tmp) ;
}
}

printf ("\nThe string: %s", num_string) ;
printf ("\nThe integer array which results is: ") ;
for ( i = 0; i < j; ++i)
printf (" %d ", values[i]) ;

return 0 ;
}``````

My output is:

The string: 1 234 23 45
The integer array which results is: 1 2 3 4 2 3 4 5
Press ENTER to continue.

Hope it helped , bye.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.