String to Number
-
look at the two functions atoi() itoa() I maybe wrong but I think the atoi() function will only convert the numerical characters from the string, and skip over the non-numerical. I don't use MFC myself, but there maybe a better way to do this using the MFC classes, I will leave that to the MFC guru's.
I know its the CRT function atoi(). But its not working for some reason. Thats why I asked for a code snippet. Someone please helpme out. Aljechin
-
I know its the CRT function atoi(). But its not working for some reason. Thats why I asked for a code snippet. Someone please helpme out. Aljechin
perhaps you could post the code you have created, this would give us a better idea of what you are trying and where you may be going wrong.
-
I have one CString which contains "MX1200" as its value. I want 1200 from this and I want to store it in a integer variable. How do I do this? Can you show a small code snippet? Aljechin Alexander
Here is a way to do that. 1. Get length of your CString. 2. Declare a char array of that length and copy your CString to that array. 3. Now the number can easily be extracted from that array. Here is the code to copy CString to char array
int size = myCString.GetLength(); char *array =(char*)malloc(size + 1); strcpy(array, myCString);
If problem remains, do let us know. Good Luck We Believe in Excellence www.aqueelmirza.cjb.net -
I know its the CRT function atoi(). But its not working for some reason. Thats why I asked for a code snippet. Someone please helpme out. Aljechin
Ok, I just tested what I thought and I was half right. atoi will stop at the first non-numerical char, so if your string start with letter you have to remove them.
char \*txt = "MX2001"; char \*buffer = new char\[10\]; int pos = 0; int result; // strip the non-numerical chars for (int i=0;i<strlen(txt);i++) { if ((txt\[i\] > 0x29)&&(txt\[i\] < 0x40)) { // if numerical buffer\[pos\] = txt\[i\]; pos++; } } buffer\[pos\] = 0x00; // add a terminating null result = atoi(buffer);
Or if your string is fairly constant and always the first two are letter you could
result = atoi(&txt[2]);
-- modified at 1:50 Wednesday 22nd March, 2006 Don't forget to use delete or you will have memory leaks;
-
I have one CString which contains "MX1200" as its value. I want 1200 from this and I want to store it in a integer variable. How do I do this? Can you show a small code snippet? Aljechin Alexander
Thank you all. Actually I had been a little dumb. I forgot I was doing a unicode build and should have used _wtoi function and not the atoi function :D. Thanks for all of your kind replies. Aljechin
-
Thank you all. Actually I had been a little dumb. I forgot I was doing a unicode build and should have used _wtoi function and not the atoi function :D. Thanks for all of your kind replies. Aljechin
Aljechin should have used _wtoi function and not the atoi function :D.
Always try to use
tchar
version of functions. Like_ttoi
. This way you don't have to worry whether it's unicode or not.
Nibu thomas Software Developer
-
Aljechin should have used _wtoi function and not the atoi function :D.
Always try to use
tchar
version of functions. Like_ttoi
. This way you don't have to worry whether it's unicode or not.
Nibu thomas Software Developer
Your welcome. I wish more people would say thankyou after they have been helped and more people to state what they did wrong or how they fixed the problem. The message boards (not only this site) are filled with questions and no answers or gratitude. It makes searching for a solution a hard task.
-
Thank you all. Actually I had been a little dumb. I forgot I was doing a unicode build and should have used _wtoi function and not the atoi function :D. Thanks for all of your kind replies. Aljechin
You can always rank helpful replies (bottom right) We Believe in Excellence www.aqueelmirza.cjb.net -- modified at 3:25 Wednesday 22nd March, 2006
-
I have one CString which contains "MX1200" as its value. I want 1200 from this and I want to store it in a integer variable. How do I do this? Can you show a small code snippet? Aljechin Alexander
CString str=_T("MX1200" ); int position; for(int i=0 , str.length(),i++) if (str.getat(i) <= '9') and (str.getat(i) >= '0') { position =i; //exit; } str =str.left(position); int yourInteger =atoi(LPCTSTR(str));
-
I have one CString which contains "MX1200" as its value. I want 1200 from this and I want to store it in a integer variable. How do I do this? Can you show a small code snippet? Aljechin Alexander
maybe it is some helpful to you http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_data_conversion.asp[^]